如何处理 iPhone 通讯录上的多个属性?

发布于 2024-09-09 00:34:58 字数 1242 浏览 9 评论 0原文

问题是我总是在 phones 变量调用的任何方法中发生运行时崩溃。 在此版本中,我在 1 (ABMultiValueCopyValueAtIndex) 处收到错误。 如果我注释掉这一行,代码就会在 2 (ABMultiValueGetCount) 处崩溃。 看来该房产已经空了。如果我 NSLog phones 变量。我得到(null)。 我在 iPhone 模拟器上测试了代码,那里有一些虚拟联系人和一些电话号码。 firstNamelastName 也很有魅力。

for(id person in people){
    NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, 0); /*1*/
/*2/  id ph, phLb;
    for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {  
        phLb = ABMultiValueCopyLabelAtIndex(phones, i);
        ph = ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"%@,%@", phLb, ph);
        CFRelease(phLb);
        CFRelease(ph);
    }
*/  
NSLog(@"%@", firstName);
NSLog(@"%@", lastName);
NSLog(@"%@", phones);
NSLog(@"%@", phone);

[firstName release];
[lastName release];
[phone release];
[phones release];

}

The problem is that I always get runtime crashes at any method invoked at phones variable.
At this version I get an error at 1 (ABMultiValueCopyValueAtIndex).
If I'll comment this line, the code crashes at 2 (ABMultiValueGetCount).
It looks like the property was empty. If I NSLog the phones variable. I get (null).
I test the code on iPhone Simulator, I have some dummy contacts there with some phone numbers.
Also firstName and lastName work like a charm.

for(id person in people){
    NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, 0); /*1*/
/*2/  id ph, phLb;
    for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {  
        phLb = ABMultiValueCopyLabelAtIndex(phones, i);
        ph = ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"%@,%@", phLb, ph);
        CFRelease(phLb);
        CFRelease(ph);
    }
*/  
NSLog(@"%@", firstName);
NSLog(@"%@", lastName);
NSLog(@"%@", phones);
NSLog(@"%@", phone);

[firstName release];
[lastName release];
[phone release];
[phones release];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-09-16 00:34:58

我认为您关于如何获取 ABMultiValueRef 的假设是错误的。我在这台计算机上没有方便的 AB 代码,但请验证 ABRecordCopyValue 是否是适当的方法。然后研究ABMultiValueRef如何访问其内容。它是一个与简单字符串完全不同的对象集。

I think your assumptions about how to procure a ABMultiValueRef are wrong. I don't have my AB code handy at this computer, but verify that ABRecordCopyValue is the appropriate method. And then do research on ABMultiValueRef on how to access its contents. It's a whole different object set than simple strings.

不及他 2024-09-16 00:34:58

代码:

addressBook = ABAddressBookCreate();
people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
CFRelease(addressBook);

释放地址簿,并且从地址簿引用的所有对象也被释放。因此,指向 MultiValueRefs 的指针在使用前已被释放。

The code:

addressBook = ABAddressBookCreate();
people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
CFRelease(addressBook);

deallocated the addressbook and all the objects that were reffered from it was released too. So the pointers to the MultiValueRefs were deallocated before use.

苍白女子 2024-09-16 00:34:58

学习使用调试器。另外,您的代码会泄漏。

Learn to use a debugger. Also, your code leaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文