从 CFStringRef 保存 NSString。弧
-
11-12-2019 - |
题
我正在努力拯救 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;
}];
}
}
解决方案
在 - 的里面 performBlock
你已分配 firstName
和 lastName
到 _dossier
而不是新创建的 dossier
目的。
其他提示
在这一行中:
firstName = (__bridge NSString*)ABRecordCopyValue(abPerson,
NSLog(firstName); kABPersonFirstNameProperty);
你的 NSLog
打断陈述。除非格式不知何故搞得非常糟糕。
不隶属于 StackOverflow