How to sort contacts in iOS 7.1

After the last update Xcode 5.1, Apple sample code to sort the address book does not work. The URL: https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/DirectInteraction.html

Code example

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); 

But now this code triggers a warning

Cast to 'void *' from smaller integer type 'ABPersonSortOrdering' (aka 'unsigned int')

In this line,

 (void*) ABPersonGetSortOrdering()) 

How do I change the code?

I really looked into the forums "Apples", "The Googled it", "Stackoverflow", and joy yet.

I hope you help me.

UPDATE

Sutures are using 64-bit, have something to do with this warning. It coincides with the inclusion of my new iPhone 5s.

+6
source share
2 answers

As you said, the problem is with the new 64-bit architecture. (void*) - 32-bit pointer to 32-bit architecture, but the 64-bit pointer to architecture with 64-bit architecture. Function ABPersonGetSortOrdering() returns a value ABPersonCompositeNameFormat , which is indicated as uint32_t in ABPerson.h. Thus, the warning lets you know that a 64-bit pointer points to a 32-bit number.

A warning can be resolved by specifying the return value to unsigned long . This is ideal because it would be 64 bits in a 64-bit architecture and to 32 bits 32-bit architecture.

 (void *)(unsigned long)ABPersonGetSortOrdering() 

Hope this helps!

+20
source

I suspect that your problem is related to the difference between 64-bit and 32-bit architectures, Xcode 5.1 supports the default 64-bit support. If pointers have 64 bits and int has 32 bits, int is too small to hold the value of the pointer.

You can disable the 64-bit compilation for your application by going to the settings of your objectives> build, but I would highly recommend that you do not. In addition, the following code works for me: it also introduces a new method of creating and correctly perform authorization checks (this ensures that the user has given permission for your application to access your contacts)

 CFErrorRef * err; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, err); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (!granted) { // No Access to address book, user has denied return; } 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); }); ); CFErrorRef * err; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, err); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (!granted) { // No Access to address book, user has denied return; } 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); }); , CFErrorRef error) { CFErrorRef * err; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, err); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (!granted) { // No Access to address book, user has denied return; } 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); }); 

Although if I wanted to be really picky, I would say that you should probably include an array of people in NSArray as follows:

 NSArray *people = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 

Then consider using your sorting methods using NSSortDescriptors , as they are likely to be faster and certainly more modern way of doing things.

0
source

All Articles