0

I'm trying to retrieve the detailedText contained in a UITableView Cell (which is a phone number "String") and then Use it in a function that will make a phone call.

  • Problem:

    My app keeps crashing with the error "fatal error: unexpectedly found nil while unwrapping an Optional value " even though there is a value inside the variable.

I'm sure that I'm doing something wrong with force unwrapping the optional

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   let cell = tableView.cellForRowAtIndexPath(indexPath)
   let getPhone = cell?.detailTextLabel?.text

   if indexPath.section == 0 && indexPath.row == 0 {
  
     if let phoneNumber = getPhone{
       openPhoneApp(phoneNumber)
     }
}

// Open Phone App
func openPhoneApp(phoneNum: String){
  UIApplication.sharedApplication().openURL(NSURL(string: "tel:\(phoneNum)")!)
}

enter image description here

Community
  • 1
  • 1
AziCode
  • 2,510
  • 6
  • 27
  • 53
  • Put a break point at at the start of your function, and check each property if I'ts nil. – MCMatan Aug 02 '16 at 18:26
  • 1
    The only force unwrap I see is in the NSURL, so you're probably creating a badly formed URL. BTW: DON'T force unwrap things – NiñoScript Aug 02 '16 at 18:29
  • Xcode is suggesting to unwrap it : "Value of optional type ' NSURL?' not unwrapped" – AziCode Aug 02 '16 at 18:33
  • 1
    I am assuming since you are using a UITableView so you have an array to populate your data, so what don't you retrieve that data at that indexPath straight from your array or data structure – Lamour Aug 02 '16 at 18:39
  • 1
    Unwrap it with a nice `if let` or `guard` statement. – NiñoScript Aug 02 '16 at 18:39
  • at error program stops just go to line where it stops and read it letter by letter, you can do that – Lu_ Aug 02 '16 at 18:40
  • @Lu_ the error message is written in the question – AziCode Aug 02 '16 at 18:52
  • @AziCode i'm talking about line the message appears not the message. Show us the screenshot where you get the error and we can see the line where it is please – Lu_ Aug 02 '16 at 18:53

3 Answers3

3

Don't use force unwrapping if you're not 100% sure it can be unwrapped. And avoid it if possible even then!

Your openPhoneApp function must receive a non-nil string, so everything's ok up to that moment.

Try to replace your force unwrap with something like this:

func openPhoneApp(phoneNum: String) {
    guard let url = NSURL(string: "tel:\(phoneNum)") else {
        print("badly formed telephone url")
        return
    }
    UIApplication.sharedApplication().openURL(url)
}

Although I'd argue that your function's name implies that it WILL open the phone app, so maybe you should just go and ask for a correctly formed URL, like this:

func openPhoneApp(phoneURL: NSURL) {
    UIApplication.sharedApplication().openURL(phoneURL)
}

and check for that kind of stuff before even calling it:

if let phone = getPhone, phoneURL = NSURL(string: "tel:\(phone)") {
    openPhoneApp(phoneURL)
}
NiñoScript
  • 4,523
  • 2
  • 27
  • 33
  • Yes, the phone numbers that I was retrieving from the cell were formated like this "(510) 222 2222". So now I'm getting rid of the spaces between numbers, and I'm also getting rid of the two parentheses around the (510) before calling "openPhoneApp". – AziCode Aug 02 '16 at 19:20
1

Did you checked the value of phoneNumber variable? it could be nil.

You can check this answer: I think this will solve your problem.

What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

specifically this answer

https://stackoverflow.com/a/35683816/5660422

Community
  • 1
  • 1
Surbhi Garg
  • 414
  • 5
  • 19
1

Please use tel:// scheme and always call

UIApplication.sharedApplication().canOpenURL

before

Lu_
  • 2,577
  • 16
  • 24
  • I don't want to use textLabel since it's just a title, I need to retrieve detailTextLabel which contains the phone number that I want to use – AziCode Aug 02 '16 at 18:37