ios - iOS 模拟器和真实设备的奇怪行为(联系人+nsdictionary)

标签 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 中。怎么了?

最佳答案

问题已解决;解析地址簿时出错;我忘记处理没有名字的联系人。

关于ios - iOS 模拟器和真实设备的奇怪行为(联系人+nsdictionary),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9658063/

相关文章:

xcode - Jenkins 与 Xcode 8 和 iOS 模拟器 : "Timed out waiting 120 seconds for simulator to boot, current state is 1"

objective-c - 在登录页面获取用户 ID

iphone - 在iOS中使用加速度计时如何平滑地移动图像对象?

ios - 在静态库中引用 PLCrashReporter 1.2 beta 2 并在客户端中构建时 undefined symbol

ios - 如何在Objective-C中将XML从Web服务解析为NSArray

objective-c - 使用自定义对象数组而不是字典重现 writeToURL/initWithContentsOfURL 行为

ios - 将array1和array 2的值合并到array3 objective-c 中

iphone - 获取当前正在播放音频的 AVPlayer

ios - 在 iPhone 上安装 Facebook 应用程序时,Facebook 登录失败

css - 相当于iOS模拟器Safari中右键 'inspect element'?