iOS模拟器和真实设备的奇怪行为(contacts + nsdictionary)

时间:2012-03-11 19:15:47

标签: ios ios-simulator nsdictionary

现在我有了这段代码:

-(void)getContacts{
ABAddressBookRef currentAddressBook = ABAddressBookCreate();
if (currentAddressBook) {
    CFArrayRef allBook = ABAddressBookCopyArrayOfAllPeople(currentAddressBook);
    if (allBook) {
        for (int index=0; index < CFArrayGetCount(allBook); index++){
            ABRecordRef currentPerson = CFArrayGetValueAtIndex(allBook, index);

            NSString *firstName = (__bridge NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);
            NSString *lastName = (__bridge NSString *)ABRecordCopyValue(currentPerson, kABPersonLastNameProperty);
            NSData *imageData = (__bridge NSData*)ABPersonCopyImageDataWithFormat(currentPerson, kABPersonImageFormatThumbnail);  
            NSMutableArray *tempArrayForPhones = [[NSMutableArray alloc] init];

            ABMultiValueRef phoneNumbersMultiValue = ABRecordCopyValue(currentPerson, kABPersonPhoneProperty);
            for(CFIndex counter = 0; counter < ABMultiValueGetCount(phoneNumbersMultiValue); counter++){
                CFStringRef currentLabel = ABMultiValueCopyLabelAtIndex(phoneNumbersMultiValue, counter);
                NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(currentLabel);

                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneNumbersMultiValue, counter);
                currentLabel = ABMultiValueCopyLabelAtIndex(phoneNumbersMultiValue, counter);
                phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(currentLabel);
                NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;
                CFRelease(phoneNumberRef);
                CFRelease(currentLabel);
                NSDictionary *tempDictForPhonew = [NSDictionary dictionaryWithObjectsAndKeys:phoneNumber,@"number",
                                                                                            phoneLabel,@"label",
                                                                                            nil];
                [tempArrayForPhones addObject:tempDictForPhonew];
            }
            NSDictionary *dictionaryWithAddressBook = [NSDictionary dictionaryWithObjectsAndKeys:firstName,@"firstName",
                                                                                                lastName,@"lastName",
                                                                                                imageData,@"image",
                                                                                                tempArrayForPhones,@"phones",
                                                                                                nil];
            tempArrayForPhones = nil;
            [dataArray addObject:dictionaryWithAddressBook];

            CFRelease(phoneNumbersMultiValue);
        }
        CFRelease(allBook);
    }
    CFRelease(currentAddressBook);
}
}

所有在模拟器上工作正常,我得到字典数组,其中包含我需要的所有字段。但是当我在真实设备(iOS 5.1)上运行代码时,字典只获得了名字和姓氏。 我试图在初始化字典时做一些nslog,并且存在带有电话和图像数据的临时字典,但不存在于最终的dataArray中。怎么了?

1 个答案:

答案 0 :(得分:1)

问题解决了;解析通讯录时出错了;我忘了处理没有名字的联系人。