1

我正在尝试保存firstNamelastname. 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;
        }];

    }
}
4

2 回答 2

2

performBlock您分配的内部,firstName而不是新创建的对象。lastName_dossierdossier

于 2012-09-30T14:43:50.777 回答
0

在这一行:

firstName = (__bridge NSString*)ABRecordCopyValue(abPerson,
        NSLog(firstName);                                                              kABPersonFirstNameProperty);

NSLog打断了陈述。除非格式以某种方式搞砸了非常糟糕。

于 2012-09-30T07:56:55.163 回答