0

I am saving the first and last name from ABpeoplepickerNavcontroller, I would like to merge the first and last name prior to saving into an array so that when i retrieve it, they would be together. The first code is the object being created:

// setting the first name
firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

// setting the last name
lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);   

Here's where I save it:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:firstName.text];
[array addObject:lastName.text];
[array addObject:addressLabel.text];

[array writeToFile:recipient atomically:NO];
[array release];

Can I save add two objects on one line? or Can I merge the objects prior to adding to array?

Thanks, and for the record...this site and the people who have helped me have been fantastic.

Michael

Michael Robinson
  • 1,439
  • 2
  • 16
  • 23

3 Answers3

2

I am not sure what you mean by merging. If you want to append one string to the other do the following:

NSString *joinedNamed = [NSString stringWithFormat:@"%@ %@", firstName.text, lastName.text]; 
Felix Lamouroux
  • 7,414
  • 29
  • 46
1

You can use the stringByAppendingString like this:

[array addObject:[firstName.text stringByAppendingString:lastName.text]];

Mihir Mathuria
  • 6,479
  • 1
  • 22
  • 15
0

From the Address Book Programming Guide for iPhone:

"However, in actual applications, the function ABRecordCopyCompositeName is the recommended way to get a person’s full name to display. It puts the first and last name in the order preferred by the user, which provides a more uniform user experience."

The solutions here will work but it's something you may not need to do at all if you just use the composite name.

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
  • Thanks for all the responses, This was the best choice...I forgot about that compositename function. 5 weeks ago sounds is an eternity when remembering code. Thanks everyone. – Michael Robinson Jan 27 '10 at 03:51