iosswiftunmanagedabaddressbookabrecordcopyvalue

Swift - Checking unmanaged address book single value property for nil


I'm relative new to iOS-Development and swift. But up to this point I was always able to help myself by some research on stackoverflow and several documentations and tutorials. However, there is a problem I couldn't find any solution yet.

I want to get some data from the users addressbook (for example the single value property kABPersonFirstNameProperty). Because the .takeRetainedValue() function throws an error if this contact doesn't have a firstName value in the addressbook, I need to make sure the ABRecordCopyValue() function does return a value. I tried to check this in a closure:

let contactFirstName: String = {
   if (ABRecordCopyValue(self.contactReference, kABPersonFirstNameProperty) != nil) {
      return ABRecordCopyValue(self.contactReference, kABPersonFirstNameProperty).takeRetainedValue() as String
   } else {
      return ""
   }
}()

contactReference is a variable of type ABRecordRef!

When an addressbook contact provides a firstName value, everything works fine. But if there is no firstName, the application crashes by the .takeRetainedValue() function. It seems, that the if statement doesn't help because the unmanaged return value of the ABRecordCopyValue() function is not nil although there is no firstName.

I hope I was able to make my problem clear. It would be great if anyone could help me out with some brainwave.


Solution

  • If I want the values associated with various properties, I use the following syntax:

    let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String
    let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String
    

    Or you can use optional binding:

    if let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String {
        // use `first` here
    }
    if let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String {
        // use `last` here
    }
    

    If you really want to return a non-optional, where missing value is a zero length string, you can use the ?? operator:

    let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String ?? ""
    let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String ?? ""