-2

I am trying to read the contact phone no. The below code works fine if the number is available but if there are no phone no assigned to the contact , it is crashing. Can Anu let me know how to handle this.

((currentContact.phoneNumbers.first?.value)! as CNPhoneNumber).stringValue
Warrior
  • 39,156
  • 44
  • 139
  • 214
  • 1
    You should see http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu – rmaddy Jan 31 '17 at 01:37

1 Answers1

3

You should learn about optional values in Swift...

if let contactPhoneNumber = currentContact.phoneNumbers.first?.value?.stringValue {
    // do something with the value
} 
else {
    // the value isn't there 
}
pbush25
  • 5,228
  • 2
  • 26
  • 35
  • 1
    No problem! Optionals are paramount to using Swift though so if you don't understand this (optional chaining and if...let) I highly suggest looking more deeply into the literature. – pbush25 Jan 31 '17 at 01:37