I have a class "Contact" which manages personal information of some people. Now i want to save some of that information using the Contact Framework introduced since iOS 9.
My Contact class has a
- Home number (huisnummer in Dutch)
- Mobile number (mobiel in Dutch)
Because some of the contacts have no Home number and some of the contacts have no mobile number it is hard for me to create the array:
// cnContact is an object of the Contact Framework class
// contact is an object of my Contact class
if let huisnummer = contact.huisnummer {
if let mobiel = contact.nummer {
cnContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMain, value: CNPhoneNumber(stringValue: huisnummer)),CNLabeledValue(label: CNLabelPhoneNumberMobile, value: CNPhoneNumber(stringValue: mobiel))]
} else {
[CNLabeledValue(label: CNLabelPhoneNumberMain, value: CNPhoneNumber(stringValue: huisnummer))]
}
} else {
if let mobiel = contact.nummer {
cnContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMobile, value: CNPhoneNumber(stringValue: mobiel))]
}
}
I have to keep checking using swifts conditionals if said number exists. But i thought since this is an inefficient way of saving those contacts that there must be a more efficient way to save this.
How can you create the cnContact's phone numbers in a more efficient way?