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.