如何使用 CNPostalAddress 创建 CNContact?
How can I create a CNContact with a CNPostalAddress?
我试过这个:
let contact = CNMutableContact()
contact.namePrefix = data["firstName"] as! String
contact.nameSuffix = data["lastName"] as! String
contact.organizationName = data["company"] as! String
contact.jobTitle = data["jobTitle"] as! String
let address = CNMutablePostalAddress()
address.street = data["street"] as! String
address.city = data["city"] as! String
address.state = data["state"] as! String
address.postalCode = data["zipcode"] as! String
address.country = data["country"] as! String
contact.postalAddresses = [address]
我收到了这个错误:
Cannot convert value of type 'CNMutablePostalAddress' to expected element type 'CNLabeledValue<CNPostalAddress>'
所以,我尝试了这个:
let contact = CNMutableContact()
contact.namePrefix = data["firstName"] as! String
contact.nameSuffix = data["lastName"] as! String
//contact.birthday =
contact.organizationName = data["company"] as! String
contact.jobTitle = data["jobTitle"] as! String
let street = CNLabeledValue(label: "street", value:data["street"] as! NSString)
let city = CNLabeledValue(label: "city", value:data["city"] as! NSString)
let state = CNLabeledValue(label: "state", value:data["state"] as! NSString)
let postalCode = CNLabeledValue(label: "zipcode", value:data["zipcode"] as! NSString)
let country = CNLabeledValue(label: "country", value:data["country"] as! NSString)
contact.postalAddresses = [street, city, state, postalCode, country]
我收到了这个错误:
Cannot convert value of type 'CNLabeledValue<NSString>' to expected element type 'CNLabeledValue<CNPostalAddress>'
而且我不知道如何创建 CNLabeledValue<CNPostalAddress>
。
你知道用 CNPostalAddress
创建 CNContact
的正确方法吗?
您误会了 CNLabeledValue
的用法。试试这个:
let contact = CNMutableContact()
contact.namePrefix = data["firstName"] as! String
contact.nameSuffix = data["lastName"] as! String
contact.organizationName = data["company"] as! String
contact.jobTitle = data["jobTitle"] as! String
let address = CNMutablePostalAddress()
address.street = data["street"] as! String
address.city = data["city"] as! String
address.state = data["state"] as! String
address.postalCode = data["zipcode"] as! String
address.country = data["country"] as! String
let labeledAddress = CNLabeledValue<CNPostalAddress>(label: CNLabelHome, value: address) //<- You can create other `CNLabeledValue<CNPostalAddress>`s with other `CNLabelXxxx`s or your custom label.
contact.postalAddresses = [labeledAddress]
我试过这个:
let contact = CNMutableContact()
contact.namePrefix = data["firstName"] as! String
contact.nameSuffix = data["lastName"] as! String
contact.organizationName = data["company"] as! String
contact.jobTitle = data["jobTitle"] as! String
let address = CNMutablePostalAddress()
address.street = data["street"] as! String
address.city = data["city"] as! String
address.state = data["state"] as! String
address.postalCode = data["zipcode"] as! String
address.country = data["country"] as! String
contact.postalAddresses = [address]
我收到了这个错误:
Cannot convert value of type 'CNMutablePostalAddress' to expected element type 'CNLabeledValue<CNPostalAddress>'
所以,我尝试了这个:
let contact = CNMutableContact()
contact.namePrefix = data["firstName"] as! String
contact.nameSuffix = data["lastName"] as! String
//contact.birthday =
contact.organizationName = data["company"] as! String
contact.jobTitle = data["jobTitle"] as! String
let street = CNLabeledValue(label: "street", value:data["street"] as! NSString)
let city = CNLabeledValue(label: "city", value:data["city"] as! NSString)
let state = CNLabeledValue(label: "state", value:data["state"] as! NSString)
let postalCode = CNLabeledValue(label: "zipcode", value:data["zipcode"] as! NSString)
let country = CNLabeledValue(label: "country", value:data["country"] as! NSString)
contact.postalAddresses = [street, city, state, postalCode, country]
我收到了这个错误:
Cannot convert value of type 'CNLabeledValue<NSString>' to expected element type 'CNLabeledValue<CNPostalAddress>'
而且我不知道如何创建 CNLabeledValue<CNPostalAddress>
。
你知道用 CNPostalAddress
创建 CNContact
的正确方法吗?
您误会了 CNLabeledValue
的用法。试试这个:
let contact = CNMutableContact()
contact.namePrefix = data["firstName"] as! String
contact.nameSuffix = data["lastName"] as! String
contact.organizationName = data["company"] as! String
contact.jobTitle = data["jobTitle"] as! String
let address = CNMutablePostalAddress()
address.street = data["street"] as! String
address.city = data["city"] as! String
address.state = data["state"] as! String
address.postalCode = data["zipcode"] as! String
address.country = data["country"] as! String
let labeledAddress = CNLabeledValue<CNPostalAddress>(label: CNLabelHome, value: address) //<- You can create other `CNLabeledValue<CNPostalAddress>`s with other `CNLabelXxxx`s or your custom label.
contact.postalAddresses = [labeledAddress]