我正在尝试保存firstName
和lastname
。
NSLog(firstName);
从地址簿中输出正确的值,但_dossier.firstName
为空。
图像正确保存。
ABAddressBookRef addressBook = ABAddressBookCreate();
for (TKAddressBook *ab in contacts) {
NSNumber *personID = [NSNumber numberWithInt:ab.recordID];
ABRecordID abRecordID = (ABRecordID)[personID intValue];
ABRecordRef abPerson = ABAddressBookGetPersonWithRecordID(addressBook, abRecordID);
NSString* firstName = nil;
NSString* lastName = nil;
// Check person image
UIImage *personImage = nil;
if (abPerson != nil && ABPersonHasImageData(abPerson)) {
firstName = (__bridge NSString*)ABRecordCopyValue(abPerson,
kABPersonFirstNameProperty);
NSLog(firstName);
lastName = (__bridge NSString*)ABRecordCopyValue(abPerson,
kABPersonLastNameProperty);
CFDataRef contactThumbnailData = ABPersonCopyImageDataWithFormat(abPerson, kABPersonImageFormatThumbnail);
personImage = [UIImage imageWithData:(__bridge NSData*)contactThumbnailData];
CFRelease(contactThumbnailData);
[_document.managedObjectContext performBlock:^() {
Dossier *dossier = [NSEntityDescription insertNewObjectForEntityForName:@"Dossier"
inManagedObjectContext:_document.managedObjectContext];
_dossier.firstName = firstName;
_dossier.lastName = lastName;
dossier.photo = personImage;
}];
}
}
答案 0 :(得分:2)
在performBlock
内,您已将firstName
和lastName
分配给_dossier
,而不是新创建的dossier
对象。
答案 1 :(得分:0)
在这一行:
firstName = (__bridge NSString*)ABRecordCopyValue(abPerson,
NSLog(firstName); kABPersonFirstNameProperty);
您的NSLog
会中断该声明。除非格式化不知何故搞砸了。