如何对 iPhone 通讯录进行排序?

发布于 2024-08-30 16:48:09 字数 76 浏览 10 评论 0 原文

我如何按名字和名字对 iPhone 通讯录进行排序(或检索排序数组)以编程方式显示姓氏?

任何帮助将不胜感激...! 谢谢

How can i sort (or retrieve sorted array of ) an iphone contact book by first name & last name programmatically ??

Any help will be well appreciated...!
Thanks

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

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

发布评论

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

评论(3

云仙小弟 2024-09-06 16:48:09

调用 ABAddressBookCopyArrayOfAllPeople() 获取地址簿中所有人员记录的数组。然后按照 文档

要对人员数组进行排序,请使用函数 CFArraySortValues,并使用函数 ABPersonComparePeopleByName 作为比较器和 ABPersonSortOrdering 类型的上下文。用户所需的排序顺序(由 ABPersonGetSortOrdering 返回)通常是首选上下文。

以下代码清单显示了对整个通讯簿数据库进行排序的示例:

ABAddressBookRef 地址簿 = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                          kCFAllocator默认,
                                          CFArrayGetCount(人),
                                          人们
                                  );

 CFArraySortValues(
        人是可变的,
        CFRangeMake(0, CFArrayGetCount(peopleMutable)),
        (CFComparatorFunction) ABPersonComparePeopleByName,
        (void*) ABPersonGetSortOrdering()
); 

CFRelease(地址簿);
CFRelease(人);
CFRelease(peopleMutable);

Call ABAddressBookCopyArrayOfAllPeople() to get an array of all person records in the address book. Then follow the documentation:

To sort an array of people, use the function CFArraySortValues with the function ABPersonComparePeopleByName as the comparator and a context of the type ABPersonSortOrdering. The user’s desired sort order, as returned by ABPersonGetSortOrdering, is generally the preferred context.

The following code listing shows an example of sorting the entire Address Book database:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                          kCFAllocatorDefault,
                                          CFArrayGetCount(people),
                                          people
                                  );

 CFArraySortValues(
        peopleMutable,
        CFRangeMake(0, CFArrayGetCount(peopleMutable)),
        (CFComparatorFunction) ABPersonComparePeopleByName,
        (void*) ABPersonGetSortOrdering()
); 

CFRelease(addressBook);
CFRelease(people);
CFRelease(peopleMutable);
蓝海 2024-09-06 16:48:09

怎么样:--

ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering

ABPerson.h 中声明

How about this:--

ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering

declared in ABPerson.h

帝王念 2024-09-06 16:48:09

我正在使用上面的代码(来自已批准的答案)从 iPhone 复制地址簿,并使用 ABPersonComparePeopleByName 对地址簿进行排序。但发现对于同一个通讯录,当iPhone的国际语言不同时,会有不同的排序结果。 假设根据不同的标准对不同的语言进行排序是合理的。所以在我们的项目中我们有“en.lproj”..“zh-hant.lproj”...“ja.lproj”,在NSCalendar中,我们还有“locale”设置。所以我在想如何设置ABPersonComparePeopleByName的标准并询问Apple。一个非常有用的回复:“排序顺序不可预测”。

苹果回复的相关部分如下:

这实际上是正常行为。以不同语言排序实际上是一个极其复杂的问题,用户的期望因语言/位置的不同而有很大差异。老实说,您最好的选择是调整您的期望并假设排序顺序是不可预测的。任何其他方法都很可能会惹恼和迷惑许多国际用户。

-凯文

Kevin Elliott,DTS 工程师,kevin_elliott@apple,com

I am using the above code (from the approved answer) to copy address book from iPhone, and also used ABPersonComparePeopleByName for sorting the address book.  But found that it
 will have different sorting results for the same address book, when the
 international language of the iPhone is different.  Suppose it is reasonable to sort different languages based on different criteria. So in our project we have "en.lproj".."zh-hant.lproj"..."ja.lproj", In NSCalendar, we also have "locale" setting. So I am thinking how to set the criteria for ABPersonComparePeopleByName and asked Apple. A very helpful reply: "a sort order is not predictable".

The relevant portion of Apple's reply is below:

This is actually normal behavior. Sorting in different languages is actually an incredibly complex issue where the expectation of the user vary widely depending on language/location. Honestly, your best option is to adjust your expectations and assume that the sort order is not predictable. Any other approach is very likely to annoy and confuse many international users.

-Kevin

Kevin Elliott, DTS Engineer, kevin_elliott@apple,com

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