我正在创建一个应用程序,我想访问联系人的名字并将它们存储到nsmutable数组中,以便我可以获得该数组的值,如array[0]
最多array[i-1]
和在表格视图中打印它们。这是我的代码:
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil)
{
contacts_Image_List=[[NSMutableArray alloc]init];
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
person.firstName = firstName;
person.lastName = lastName;
person.fullName = fullName;
// person.userThumb[i]=firstName;
//[person.userThumb[i] addObject:@"firstName"];
//above line gives null
NSLog(@"%@",person.userThumb[i]);
我的可变数组在Person.h
类中。
答案 0 :(得分:1)
在每次迭代中,只需执行:
[person.yourMutableArray addObject:fullName]
确保已经分配了MutableArray,并且只分配一次。