0

I was wondering if there was a way to catch the NSInvalidArgument error that is thrown when a NSExpression is invalid; such as: “4+”.

I have trying doing a guard, if let, but both seem to fail and the exception is still thrown. I know that there will be cases that the error will be thrown and I don’t want the app to crash each time - there’s no way to prevent it.

Any help is greatly appreciated. I did see that you can do it in Objective-C but I couldn’t find a good tutorial or explanation on how to create the header files for Swift.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Torewin
  • 887
  • 8
  • 22
  • So I guess that means there is not a way to do this in Swift. – Torewin Oct 10 '17 at 04:23
  • just have a look at the marked duplicate answer where can can simply use exception habdling with a try catch block – AlexWoe89 Oct 10 '17 at 04:43
  • 1
    Swift has Try, Catch blocks also - that answer is from 3 years ago and 2 Swift versions. Is there not a Swift 3 or 4 way to do the same thing? – Torewin Oct 10 '17 at 04:48
  • @Torewin: The Swift try/catch has nothing to do with Objective-C exceptions, which (still) cannot be catched in Swift 3/4, so that answer is still valid. – It is a pity that NSExpression does not throw a Swift error for invalid input. – Martin R Oct 10 '17 at 05:26
  • Man that sucks! They really need to add that. – Torewin Oct 10 '17 at 13:38

0 Answers0