我想在 iPhone 通讯簿中搜索特定电话号码,然后检索联系人姓名。我目前正在遍历所有联系人并提取多值属性并与值进行比较。这花费了太多时间。我已经阅读了 Apple 地址簿指南,他们说:
"accomplish other kinds of searches, use the function ABAddressBookCopyArrayOfAllPeople and then filter the results using the NSArray method filteredArrayUsingPredicate:."
谁能给我一个具体如何做到这一点的例子?
谢谢。
最佳答案
如果你想在联系人中搜索电话号码,那么我会告诉你一个建议。
1.获取所有联系人
NSArray *thePeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
2.从联系人数组(thePeoples)创建另一个数组(records),
records:[ record1, record2, ....recordN ]
record: {name:"myContactName", phoneNumber:"1234567890"}
3.用谓词搜索mutableArray(records)。
NSPredicate * myPredicate = [NSPredicate predicateWithFormat:@"record.phoneNumber contains %@",string];
NSArray * filteredArray = [records filteredArrayUsingPredicate:myPredicate];
这只是您的解决方案的一个简单示例,请记住 phoneNumber 是一个多值字段。因此,我们将在模型类变量中包含一个数组作为电话号码。
关于iphone - 搜索 AB 地址簿 iOS SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5153377/