-2

fatal error: unexpectedly found nil while unwrapping an Optional value

function signature specialization of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)

My code is:

addressBook.fieldsMask = APContactField.Default
        addressBook.sortDescriptors = [NSSortDescriptor(key: "name.firstName", ascending: true)]
        addressBook.filterBlock = {(contact: APContact!) -> Bool in
            print(contact.phones)

            return contact.phones!.count > 0
        }


        addressBook.loadContacts({ (contacts: [APContact]?, error: NSError?) in
            if (contacts != nil) {
                self.allContacts.removeAll(keepCapacity: false)
                self.tableView.tableHeaderView = self.resultSearchController.searchBar
                let newContacts = contacts as [APContact]!
                //Validate Phone Number

                newContacts.map({ (contact: APContact) -> Void in
                    if self.isValidNumberContact(contact) {
                        self.allContacts.append(contact)
                        print(self.allContacts)
                    }
                })
                self.callContactsWebservice(WithPhoneNumbers: self.allContacts)
                completion(contactsFound: true)
            } else {
                completion(contactsFound: false)
            }
        })
    case .Denied:
        showPermisisonDeniedAlert()
    default:
        print("", terminator: "")
    }
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • 2
    Possible duplicate of [What does an exclamation mark mean in the Swift language?](http://stackoverflow.com/questions/24018327/what-does-an-exclamation-mark-mean-in-the-swift-language) – Eric Aya Nov 30 '15 at 10:29

1 Answers1

0

Use guard to make sure the value is not nil:

addressBook.fieldsMask = APContactField.Default
    addressBook.sortDescriptors = [NSSortDescriptor(key: "name.firstName", ascending: true)]
    addressBook.filterBlock = {(contact: APContact!) -> Bool in
        print(contact.phones)
        guard let phones = contact.phones else {
         return
         }
        return phones.count > 0
    }


    addressBook.loadContacts({ (contacts: [APContact]?, error: NSError?) in
          guard let phones = contacts as? [APContact] else {
         completion(contactsFound: false)
         return
         }
            self.allContacts.removeAll(keepCapacity: false)
            self.tableView.tableHeaderView = self.resultSearchController.searchBar
            let newContacts = contacts as [APContact]!
            //Validate Phone Number

            newContacts.map({ (contact: APContact) -> Void in
                if self.isValidNumberContact(contact) {
                    self.allContacts.append(contact)
                    print(self.allContacts)
                }
            })
            self.callContactsWebservice(WithPhoneNumbers: self.allContacts)
            completion(contactsFound: true)
    })
case .Denied:
    showPermisisonDeniedAlert()
default:
    print("", terminator: "")
}
vale
  • 1,376
  • 11
  • 25