-3

I am new trying to understand debugging and I have a challenge I am working through. I have queried for a solution to this issue and not found it within StackOverflow. I am tying to better understand what is happening here and why this crash occurs.

I have tracked down the bug via print statements in my source code. I added a print statement to the beginning of this function to confirm that the block is being reached.

@IBAction func bugTypeSelected(_ sender: UIButton) {
    print("bugTypeSelected reached")
    bugFactory.currentBugType = BugFactory.BugType(rawValue: Int(sender.currentTitle!)!)!
    self.dismiss(animated: true, completion: nil)
}

When I run the app and click one of the bugs in the settings modal, the print statement is printed to the console and then the app crashes. Xcode is telling me that the issue lies within this line:

bugFactory.currentBugType = BugFactory.BugType(rawValue: Int(sender.currentTitle!)!)!

The error reads

“Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.”

So, I understand that using a nil value will crash the app. I also understand that I am working with optionals here. I am stuck on what to do next.

Andrew Tuzson
  • 619
  • 8
  • 28
  • 2
    You should never use the `!` operator in Swift. Use `guard let` or `if let` and add error output in the case where the value cannot be unwrapped. This way you will find your error. – dasdom Nov 28 '17 at 11:05
  • 1
    @dasdom *You should never use the ! operator in Swift* is too black and white. There are many *levels of grey* (cases) where forced unwrapping is absolutely safe and will even reveal design errors. But you are right in **this** particular case. – vadian Nov 28 '17 at 11:18
  • @vadian In my opinion it shouldn't had been added to Swift in the first place. But it needed to be added because of ObjC. – dasdom Nov 28 '17 at 11:22
  • I understand that Swift is a descendant of ObjC, but I am using Swift and Xcode for this iOS project. I want to make sure I am using StackOverflow correctly. Also, it is my understanding that optionals are a Swift specific feature; is that not the case? – Andrew Tuzson Nov 28 '17 at 11:25

1 Answers1

0

The problem with that line is you are force to unwarp values if value nil or not a data type which you force to caste means app will crash

Instead of force to unwarp use optional unwarp like below

if sender.currentTitle != nil {
  if let requiredIntValue = Int(sender.currentTitle!){
    if let bugType = BugFactory.BugType(rawValue: requiredIntValue)?{
       bugFactory.currentBugType = bugType
    }
  }
}

Hope this will help you

Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28
  • Thank you Ganesh. I see where you are unwrapping the optional, but I don't understand your first two lines. Also, this throws an error on line 2 and 3. The error is the same for both lines and it reads: Use of unresolved identifier 'requiredIntValue' What am I missing? – Andrew Tuzson Nov 28 '17 at 11:17
  • answer updated check it once – Ganesh Manickam Nov 28 '17 at 11:19
  • That works, thank you. I don't quite understand the use of optional chaining here. – Andrew Tuzson Nov 28 '17 at 11:23
  • If you consider that your problem is solved, you can accept answer, this will mark the question as answered. Cheers! – Ganesh Manickam Nov 28 '17 at 11:24