I'm working with contacts in my app, user can choose a contact name… but if first name or last name being empty, I will get this common error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I know my question maybe duplicate, but I have read some articles but I couldn't find out how to solve mine.
Here is my code:
let firstName: ABMultiValueRef? =
ABRecordCopyValue(person,
kABPersonFirstNameProperty).takeRetainedValue() as ABMultiValueRef
let lastName: ABMultiValueRef? =
ABRecordCopyValue(person,
kABPersonLastNameProperty).takeRetainedValue() as ABMultiValueRef
titleField.text = ("\(firstName) \(lastName)")
I want to fill textfield anyway.
EDIT:
I have found this solution from related question:
var name:String = ""
if let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String {
name += first
}
if let last = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String {
name += last
}
titleField.text = name
}