gpt4 book ai didi

swift - 无法在 Swift 中更新联系人的电话号码

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:13 25 4
gpt4 key购买 nike

在发布这个问题之前浏览了很多博客、问题和文章。我无法使用 Swift 中的联系人框架更新联系人的电话号码。

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)
}
}

enter image description here

最佳答案

找到罪魁祸首

更改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>

关于swift - 无法在 Swift 中更新联系人的电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49737676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com