I am trying to get the composite name of contacts but when both first and last name of the contact is empty this code crashes. How do i test if the composite name property is nil? Also why is not optional binding working in this case?
let allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray
for person: ABRecordRef in allPeople {
var compositeName: String? = ""
// Crashes on the next line
if let cName = ABRecordCopyCompositeName(person).takeRetainedValue() as? String {
compositeName = cName
} else {
compositeName = ""
}
let phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
for counter in 0..<ABMultiValueGetCount(phones) {
let phone = ABMultiValueCopyValueAtIndex(phones, counter).takeRetainedValue() as! String
contactArray.append(nameNumber(name: compositeName!, number: phone))
}
}