У меня проблема с моим приложением. Я использую func importPhoneBookContact
и получаю:
Неустранимая ошибка: неожиданно обнаружен ноль при развертывании необязательного значения
когда я запускаю приложение по адресу:
cnContacts.remove(at: cnContacts.index(of: contact)!)
Как отправить ноль строку в мою функцию?
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
}
}