在发布此问题之前,浏览了许多博客,问题和文章。我无法使用Swift中的Contacts框架更新联系人的电话号码。
let store = CNContactStore()
OperationQueue().addOperation{[store] in
let predicate = CNContact.predicateForContacts(matchingName: "XYZ")
let toFetch = [CNContactPhoneNumbersKey, CNContactGivenNameKey]
do{
let contacts = try store.unifiedContacts(matching: predicate,
keysToFetch: toFetch as [CNKeyDescriptor])
for Contact in contacts{
let XYZ = Contact.mutableCopy() as! CNMutableContact
print("Name: ", XYZ.givenName)
print(XYZ.phoneNumbers.count)
for CNPhoneNumber in XYZ.phoneNumbers{
print("Phone number: ", CNPhoneNumber.value)
var text = CNPhoneNumber.value.stringValue
let matchText = text.starts(with: "011 91")
if(matchText == true){
let start = text.index(text.startIndex, offsetBy: 0);
let end = text.index(text.startIndex, offsetBy: 8);
text.replaceSubrange(start..<end, with: "+91")
print("Replaced number: ",text)
} else{
print("Phone number is not starting with 011 91")
}
let phoneNumber = CNLabeledValue(label: CNLabelPhoneNumberiPhone, value:CNPhoneNumber(stringValue: text))
XYZ.phoneNumbers[0] = phoneNumber
let req = CNSaveRequest()
req.update(XYZ)
try store.execute(req)
}
}
}
catch let err{
print(err)
}
}
答案 0 :(得分:1)
找到了罪魁祸首
将CNPhoneNumber
更改为任何其他有效的文字
for CNPhoneNumber in daddy.phoneNumbers
到
for phoneNumber in daddy.phoneNumbers
//change the rest of the for-in loop accordingly
原因是,上部代码部分将CNPhoneNumber
声明为CNLabeledValue<CNPhoneNumber>
对象,并且错误地告诉您,您试图为非函数类型对象CNLabeledValue<CNPhoneNumber>
<调用一个值< / p>