gpt4 book ai didi

swift - fatal error : Unexpectedly found nil while

转载 作者:行者123 更新时间:2023-11-30 11:22:11 26 4
gpt4 key购买 nike

我的应用程序遇到问题。我正在使用 func importPhoneBookContact,并获取:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

当我运行应用程序时:

cnContacts.remove(at: cnContacts.index(of: contact)!)

如何将 nil 字符串发送到我的函数?

func importPhoneBookContact(){
let keysToFetch = [
CNContactGivenNameKey, // Formatter.descriptorForRequiredKeys(for: .fullName),
CNContactPhoneNumbersKey,
CNContactImageDataAvailableKey,
CNContactThumbnailImageDataKey] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: keysToFetch)
var cnContacts = [CNContact]()
var phoneNumbers = [String]()
let phoneNumberKit = PhoneNumberKit()
let store = CNContactStore()
do {
try store.enumerateContacts(with: request){ (contact, cursor) -> Void in
cnContacts.append(contact)
}

if cnContacts.count > 0 {
for contact in cnContacts {
if contact.phoneNumbers.count > 0 {
for phone in contact.phoneNumbers{
if PFUser.current() == nil {
return
}

var phoneNumber = phone.value.stringValue
phoneNumber = phoneNumber.components(separatedBy: .whitespaces).joined()

if (phoneNumber.count) > 3 {
if String(phoneNumber[..<phoneNumber.index(phoneNumber.startIndex, offsetBy: 2)]) == "00" {
phoneNumber = "+"+String(phoneNumber[phoneNumber.index(phoneNumber.startIndex, offsetBy: 2)...])
} else if String(phoneNumber[..<phoneNumber.index(phoneNumber.startIndex, offsetBy: 1)]) != "+" {
if let code = (PFUser.current() as! User).countryCode {
phoneNumber = "+"+String(describing: phoneNumberKit.countryCode(for: code)!)+phoneNumber
}
}
}

if phoneNumbers.contains(phoneNumber){
cnContacts.remove(at: cnContacts.index(of: contact)!)
} else{
phoneNumbers.append(phoneNumber)
}
}
}
}

AppDelegate.contacts = cnContacts
print("phone book contacts: ", AppDelegate.contacts.count)
}
} catch let error {
NSLog("Fetch contact error: \(error)")
AppDelegate.contactsImported = true
}
}

最佳答案

正如 @John Montgomery 已经指出的那样,永远不要在循环中改变数组。因此,我建议不要在循环时删除对象,而是维护要删除的对象数组,并在最后删除它们。以下是我的更新版本。

func importPhoneBookContact(){
let keysToFetch = [
CNContactGivenNameKey, // Formatter.descriptorForRequiredKeys(for: .fullName),
CNContactPhoneNumbersKey,
CNContactImageDataAvailableKey,
CNContactThumbnailImageDataKey] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: keysToFetch)
var cnContacts = [CNContact]()
var phoneNumbers = [String]()
let phoneNumberKit = PhoneNumberKit()
let store = CNContactStore()
var contactsToRemove = [CNContact]()
do {
try store.enumerateContacts(with: request){ (contact, cursor) -> Void in
cnContacts.append(contact)
}

for contact in cnContacts {
if contact.phoneNumbers.count > 0 {
for phone in contact.phoneNumbers{
if PFUser.current() == nil {
return
}

var phoneNumber = phone.value.stringValue
phoneNumber = phoneNumber.components(separatedBy: .whitespaces).joined()

if phoneNumber.count > 3 {
if phoneNumber.prefix(2) == "00" {
phoneNumber = "+"+String(phoneNumber[phoneNumber.index(phoneNumber.startIndex, offsetBy: 2)...])
} else if phoneNumber.prefix(1) != "+",
let user = PFUser.current(),
let code = user.countryCode,
let countryCode = phoneNumberKit.countryCode(for: code) {
phoneNumber = "+"+countryCode+phoneNumber
}
}

if phoneNumbers.contains(phoneNumber) {
contactsToRemove.append(contact)
} else {
phoneNumbers.append(phoneNumber)
}
}
}
}
cnContacts = cnContacts.filter({ contactsToRemove.contains($0) })
AppDelegate.contacts = cnContacts
print("phone book contacts: ", AppDelegate.contacts.count)
} catch let error {
NSLog("Fetch contact error: \(error)")
AppDelegate.contactsImported = true
}
}

关于swift - fatal error : Unexpectedly found nil while,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51145179/

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