如何处理 iPhone 通讯录上的多个属性?
问题是我总是在 phones
变量调用的任何方法中发生运行时崩溃。 在此版本中,我在 1
(ABMultiValueCopyValueAtIndex
) 处收到错误。 如果我注释掉这一行,代码就会在 2
(ABMultiValueGetCount
) 处崩溃。 看来该房产已经空了。如果我 NSLog
phones
变量。我得到(null)
。 我在 iPhone 模拟器上测试了代码,那里有一些虚拟联系人和一些电话号码。 firstName
和 lastName
也很有魅力。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您关于如何获取 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.
代码:
释放地址簿,并且从地址簿引用的所有对象也被释放。因此,指向 MultiValueRefs 的指针在使用前已被释放。
The code:
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.
学习使用调试器。另外,您的代码会泄漏。
Learn to use a debugger. Also, your code leaks.