How can i put an exception on EXC_BAD_INSTRUCTION?

前端 未结 2 431
暗喜
暗喜 2021-01-29 01:37

I\'m trying to put an exception for the following line:

let someData Int = Int(anArray[0])!

I want the exception so that it ignores it if its a

2条回答
  •  执念已碎
    2021-01-29 02:04

    You missed the proper solution:

    if let someData = Int(anArray[0]) {
        // someData is a valid Int
    }
    

    Or you can use guard:

    guard let someData = Int(anArray[0]) else {
        return // not valid
    }
    
    // Use someData here
    

    Note the complete lack of the use of !. Don't force-unwrap optionals unless you know it can't fail.

提交回复
热议问题