我正在实现 public class func JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions) throws -> AnyObject
但不知道我可以捕获哪些异常?我的同事建议在我发现错误时进行调试。但是在运行代码之前必须有另一种方法来找出答案吗?
最佳答案
这里的 throws 只是 NSError 的包装
通常我这样使用这个函数:
if let yourObject = (try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as? TObjectTypeYouExpected
{
// other code here
}
你也可以这样使用它:
do {
yourObject = try NSJSONSerialization.JSONObjectWithData(data, options: options)
} catch let error as NSError {
print("Some error: \(error)")
data = nil
}
关于swift - 我怎么知道要在 Swift 中抛出的核心函数中捕获哪些异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36908931/