Search code examples
sortingswiftaddressbook

Swift AddressBook - copy all people in source with sort ordering issue


here is the issue I'm getting. Any help is much appreciated.

enter image description here

Here's the code:

var addressBook: ABAddressBookRef = {
    var error: Unmanaged<CFError>?
    return ABAddressBookCreateWithOptions(nil,
    &error).takeRetainedValue() as ABAddressBookRef
}()    

var source = ABAddressBookCopyDefaultSource(addressBook)!

var allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source: source, sortOrdering: kABPersonSortByFirstName)

Error message: Cannot convert the expression's type '(ABAddressBookRef, source: @lvalue Unmanaged, sortOrdering: Int)' to type '$T4'


Solution

  • Two errors: ABAddressBookCopyDefaultSource() returns Unmanaged<ABRecord>!, so you have to call takeRetainedValue() on the returned value.

    And the last argument to ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering() must be converted to the expected type ABPersonSortOrdering:

    var source: ABRecord = ABAddressBookCopyDefaultSource(addressBook).takeRetainedValue()
    
    var allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook,
        source, ABPersonSortOrdering(kABPersonSortByFirstName))