ios - Swift - Checking unmanaged address book single value property for nil -
i'm relative new ios-development , swift. point able myself research on stackoverflow , several documentations , tutorials. however, there problem couldn't find solution yet.
i want data users addressbook (for example single value property kabpersonfirstnameproperty
). because .takeretainedvalue()
function throws error if contact doesn't have firstname value in addressbook, need make sure abrecordcopyvalue()
function return value. tried check in closure:
let contactfirstname: string = { if (abrecordcopyvalue(self.contactreference, kabpersonfirstnameproperty) != nil) { return abrecordcopyvalue(self.contactreference, kabpersonfirstnameproperty).takeretainedvalue() string } else { return "" } }()
contactreference
variable of type abrecordref!
when addressbook contact provides firstname value, works fine. if there no firstname, application crashes .takeretainedvalue()
function. seems, if statement doesn't because unmanaged return value of abrecordcopyvalue()
function not nil although there no firstname.
i hope able make problem clear. great if me out brainwave.
if want values associated various properties, use following syntax:
let first = abrecordcopyvalue(person, kabpersonfirstnameproperty)?.takeretainedvalue() as? string let last = abrecordcopyvalue(person, kabpersonlastnameproperty)?.takeretainedvalue() as? string
or 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 want return non-optional, missing value 0 length string, can use ??
operator:
let first = abrecordcopyvalue(person, kabpersonfirstnameproperty)?.takeretainedvalue() as? string ?? "" let last = abrecordcopyvalue(person, kabpersonlastnameproperty)?.takeretainedvalue() as? string ?? ""
Comments
Post a Comment