Calling NSException.raise() in Swift

后端 未结 3 1521
遥遥无期
遥遥无期 2021-02-20 06:26

I\'m trying to raise an exception in Swift by calling NSException.raise(). The definition is:

class func raise(_ name: String!, format format: String!, arguments         


        
3条回答
  •  别那么骄傲
    2021-02-20 07:09

    The problem seems to have been that I didn't treat the error as an optional. The following works:

    var error: NSError?
    NSException.raise("Exception", format:"Error: %@", arguments:getVaList([error!]))
    

    Or you could do the following in case error is nil:

    NSException.raise("Exception", format:"Error: %@", arguments:getVaList([error ?? "nil"]))
    

提交回复
热议问题