1

I have A weird problem, I am trying to get all contacts from IOS addressbook API, while I am trying to get all the values of properties (First name, Last name, Emails and phone numbers of each contact I am g etting "fatal error: unexpectedly found nil while unwrapping an Optional value"), just when i run the the command on my device, but when I am running the command on the xcode simulator everything works fine?

here is my sample code:

func getContactNames() {

        let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray



        for person in people {


        let firstName = ABRecordCopyValue(person,
                kABPersonFirstNameProperty).takeRetainedValue() as! String

        let lastName = ABRecordCopyValue(person,
               kABPersonLastNameProperty).takeRetainedValue() as! String


        let email: ABMultiValueRef = ABRecordCopyValue(person,kABPersonEmailProperty).takeRetainedValue()

        println("First name = \(firstName)")
        println("Last name = \(lastName)")
        println("Email = \(email)")

    }
}
  • 1
    you tagged this with `swift2` but I see `println` instead of Swift2's `print` in your code block. Which version of Xcode are you using? Also, what line is the crash happening on? – Michael Dautermann Aug 11 '15 at 11:26
  • Ask the user for access to their address book Well answered here http://stackoverflow.com/questions/24752627/accessing-ios-address-book-with-swift-array-count-of-zero – Prabhu.Somasundaram Aug 11 '15 at 12:23

1 Answers1

0

You may find the problem is with your use of as! String. If any entries in your address book do not have a First Name or a Last Name, then you are attempting to retain a nil, hence the error.

My solution to this problem has been to use code like this:

public func stringPropertyValue(record: ABRecord, id:ABPropertyID) -> String! {
    var result: String! = nil
    if let v = ABRecordCopyValue(record, id) {
        result = v.takeRetainedValue() as! String
    }
    return result
}

The you can set up variables like this:

let firstName = stringPropertyValue(person, id:kABPersonFirstNameProperty)
let lastName = stringPropertyValue(person, id:kABPersonLastNameProperty)

The resulting variables are implied optionals, but they won't cause the unwrapping errors. The above function can be easily reworked into a class structure along the lines of the new Contacts framework in iOS 9 / Xcode 7

pbc
  • 515
  • 4
  • 11
  • User claims that his code was working fine with simulator. It should be problem with the platform and privacy settings. – Prabhu.Somasundaram Aug 11 '15 at 12:25
  • @Prabhu.Somasundaram is wrong: it has nothing to do with platform and privacy settings and everything to do with the actual data in the database. The reason it probably worked in the simulator is that all the entries in the test database had first and last names, so the fault did not occur. Live address books will probably contain records which don't have first **and** last names and that causes the problem. Using "as!" against a nil throws an exception: if either the first or last name are nil, it crashes. – pbc Nov 26 '15 at 00:02