Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort contacts in iOS 7.1

After the last update to Xcode 5.1, the Apple's example code for sorting Address Book stopped working. URL: https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/DirectInteraction.html

Example Code

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 raises a warning

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

In this line

(void*) ABPersonGetSortOrdering())

How should I modified this code?

I actually looked into Apples' forums, Googled it, Stackoverflowed it, and no joy yet.

Hope you can help me.

UPDATE

It seams using 64bit has something to do with this warning. It coincide with the inclusion of the my new iPhone 5s.

like image 663
dequin Avatar asked Mar 17 '14 20:03

dequin


People also ask

How do you sort Contacts on iPhone?

You can choose how to sort your contacts, like alphabetically by first or last name. Just go to Settings > Contacts and choose from the following: Sort Order: Sort your contacts alphabetically by first or last name. Display Order: Show contacts' first names before or after last names.

How do I get my iPhone Contacts to sort by first name?

Tap Mail, Contacts, Calendars, scroll down to the Contacts section, and peek at Sort Order. Then tap Last, First or First, Last. You can also determine whether you want to display a first name or last name first. Tap Display Order and then choose First, Last, or Last, First.


1 Answers

As you stated, the problem is with the new 64 bit architecture. (void*) is a 32 bit pointer for a 32 bit architecture, but a 64 bit pointer for a 64 bit architecture. The function ABPersonGetSortOrdering() returns a value of type ABPersonCompositeNameFormat which is specified as a uint32_t in ABPerson.h. So the warning is letting you know that a 64 bit pointer is pointing to a 32 bit number.

The warning can be eliminated by typecasting the return value to an unsigned long. This is perfect because it will be 64 bits on a 64 bit architecture and 32 bits on a 32 bit architecture.

(void *)(unsigned long)ABPersonGetSortOrdering()

Hope this helps!

like image 110
Brent Avatar answered Nov 03 '22 12:11

Brent