The code that used to work in iOS 9 was:
var valuesArray : [CNLabeledValue] = []
But I can't figure out how to do it in Swift 3.
The code that used to work in iOS 9 was:
var valuesArray : [CNLabeledValue] = []
But I can't figure out how to do it in Swift 3.
This is the solution:
var phoneNumbers : [CNLabeledValue<CNPhoneNumber>] = []
As OOPer pointed out in this post:
CNLabeledValue
's generic parameter is declared as<ValueType : NSCopying, NSSecureCoding>
. So, in this case, you can choose any type which conforms toNSCopying
andNSSecureCoding
.NSString
does andString
does not.
something like this (with example to fill out phone number):
let phonesArray : [Phones] = phones!
var phonesToAdd = [CNLabeledValue]()
for phone in phonesArray
{
if let phoneT = phone.phoneType
{
if phoneT.lowercaseString == "mobile"
{
let mobilePhone = CNLabeledValue(label: "mobile",value: CNPhoneNumber(stringValue: phone.phone))
phonesToAdd.append(mobilePhone)
}
if phoneT.lowercaseString == "landline"
{
let landlinePhone = CNLabeledValue(label: "landline",value: CNPhoneNumber(stringValue: phone.phone))
phonesToAdd.append(landlinePhone)
}
}
}
contactData.phoneNumbers = phonesToAdd