0

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'

Jake M.
  • 105
  • 8

1 Answers1

1

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))
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks!! One more issue... how do I convert an unmanaged to a CFArray or even better, an NSArray? – Jake M. Apr 04 '15 at 19:06
  • because allPeople is currently of type unmanaged which I cant iterate through with a loop – Jake M. Apr 04 '15 at 19:07
  • @user3471847: Perhaps `ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(...).takeRetainedValue() as [ABRecord]` ? – Martin R Apr 04 '15 at 19:09
  • @user3471847: Which Xcode version are you using? It compiles in my Xcode 6.2. – Martin R Apr 04 '15 at 19:11
  • 6.1 - will update now. Thanks for the help, much appreciated – Jake M. Apr 04 '15 at 19:14
  • @MartinR - Good answer, but shouldn't you really use optional chaining (e.g. `?.takeRetainedValue()`, combined with optional binding, to gracefully handle `nil` values? – Rob Apr 04 '15 at 22:40
  • @Rob: I am not 100% sure, but the ABAddressBookCopyDefaultSource() documentation does *not* state that it can return NULL/nil, therefore I assumed that it always succeeds. ABAddressBookGetSourceWithRecordID(), for example, *can* return NULL. I assume that the address book functions haven't yet been audited for optional conformance, they all return implicitly unwrapped optionals. – Martin R Apr 05 '15 at 05:16
  • "ABAddressBookCopyDefaultSource" not getting all Contact...could any one assist me here? – Saumil Shah Oct 15 '15 at 07:35