1

I am getting these errors often when a function I call uses something like:

 optionalVar!

"Unexpectedly found nil while unwrapping an Optional"

I am unsure how to deal with functions that fail sometimes if I don't always have control over the inner code?

Is there a way to protect around such crashes? In most languages I could put try catches around most things.

When I do something like:

if let result = Blah.someExternalFunction(html: "some bad html") { } 

This can still fail inside "someExternalFunction", even after trying to add try? in front of it.

Thanks

JasonAddFour
  • 153
  • 1
  • 1
  • 10
  • Show a complete example of your relevant code and how it is called. And thoroughly read [What does “fatal error: unexpectedly found nil while unwrapping an Optional value” mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu?rq=1) – rmaddy May 30 '18 at 00:02
  • You *cannot* catch that from outside the function, see for example https://stackoverflow.com/q/38737880/1187415 – Martin R May 30 '18 at 00:05
  • Sounds like you need to fix `someExternalFunction` or ask its developer to fix it. – rmaddy May 30 '18 at 00:25

1 Answers1

0

What you're looking for is if let

If you have an optional value, you can simply do something like this to "try" it:

if let val = optionalVar{
    //val is already unwrapped
}
else{//it was nil}

Another option is to use a guard statement. It works similarly.

guard let val = optionalVar 
else{
    //the value is nil, so you need to exit the current function
    return
}
//'val' is now unwrapped for any code below the guard-else statement
LulzCow
  • 1,199
  • 2
  • 9
  • 21
  • I typically use that but sadly it doesn't work when I do something like: if let result = Blah.someExternalFunction(html: "some bad html") { } .. this can still fail inside "someExternalFunction". Does that make sense? I tried putting a try? in front of it but that did not help. – JasonAddFour May 30 '18 at 00:07
  • @JasonAddFour oh I see, I misunderstood your question. In this case, you'd probably need to fix whatever was force unwrapping a nil value inside the outside function, or if it's an open source project, open an bug/PR and have the author fix it. Is the outside function something you have control over? – LulzCow May 30 '18 at 00:09
  • My understanding from researching this is that if I don't have control over "someExternalFunction" then there is no way in Swift to protect against forced unwrapping that could be nil & thus crashing my app? This seemed strange to me since it is such a crucial part of programming to be able to protect against such bugs that I thought I must be missing something? What if I am working with large libraries that have edge cases that I can't test locally and my app randomly crashes on the wild? There's no way to wrap such functions in try catch clauses? – JasonAddFour May 30 '18 at 00:17
  • @JasonAddFour No, unfortunately there isn't. I believe that if it's an ObjC exception, you can catch it, but for some reason, Swift doesn't allow for this. I've rarely used a third-party library where something like this was an issue however. Swift makes handling most common errors very easy, so hopefully you won't run into many third party libraries that consistently crash your app, and if it's you're own code, then you can just fix the issue that's throwing the exception. I do think there has been some debate about whether to add what you're looking for, but I don't know the status of that – LulzCow May 30 '18 at 00:53