1

In Xcode7/Swift 2, the call

ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue()

returns a non-null CFArrayRef, but when I cast it "as? NSArray" or "as? [ABRecordRef]" (which used to work) I now get nil. I understand that we should move to the Contacts framework, but the old way should still work for a while. What's up?

Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55

1 Answers1

3

Correct syntax for Swift 2 requires you to string two casts in a row:

let rawCFArrayRef =
  ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue()

let swiftArray = rawCFArrayRef as? NSArray? as? [ABRecordRef]

Note that the ? on NSArray? is crucial.

Andrew Duncan
  • 3,553
  • 4
  • 28
  • 55
  • Same thing for strings `ABRecordCopyCompositeName(selectedPerson)?.takeRetainedValue() as NSString? as? String`. Irritating! This should have been enforced. – Andreas Sep 21 '15 at 20:17