我正在閱讀地址簿聯繫人......一切都很順利,直到我測試了一個沒有 的聯繫人名字(因爲我只能通過電子郵件或電話或觀看者創建聯繫人... )。 代碼(減少)是這樣的:kABPersonFirstNameProperty ... trowing EXC_BAD_ACCESS
- (NSMutableArray *) getContactsInfo {
NSMutableArray *contactsList = [[NSMutableArray alloc] init];
localAddressBook = ABAddressBookCreate();
int contactsLength = (int)ABAddressBookGetPersonCount(localAddressBook);
if (contactsLength < 1)
return nil;
for(int currentContact=1; currentContact < (contactsLength + 1); currentContact++) {
ABRecordRef person = ABAddressBookGetPersonWithRecordID(localAddressBook,(ABRecordID) currentContact);
firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"%@", firstName);
[contactsList addObject:firstName];
CFRelease(person);
}
return contactsList;
}
和輸出我得到的是這樣的:
2010-02-15 14:16:25.616 testApp[7065:207] Contact0
2010-02-15 14:16:25.618 testApp[7065:207] Contact1
2010-02-15 14:16:25.619 testApp[7065:207] Contact2
Program received signal: 「EXC_BAD_ACCESS」.
我有一個名和姓 而一個只用姓氏創建3個觸點,出於測試目的。
看來我可以正確讀取任何屬性,如電子郵件或地址與數組......但是當一個聯繫人缺少名字屬性應用程序崩潰。
控制檯說了什麼? – kennytm 2010-02-15 19:16:57