Reputation: 8224
Each guide I find on swift 2.0 error handling shows handling errors on a custom class. I know how to do, try, catch but what I don't know is what to catch. I know I'm testing for certain enum to indicate the error but where or how do i find these error enums if i did not create the class?
Im using
class func JSONObjectWithData(_ data: NSData,
options opt: NSJSONReadingOptions) throws -> AnyObject
so it says it throws and I would like to handle that but what does it throw? how do I know what enums to catch in the catch block? No doubt I'm missing something obvious but you know what it's like when you just can't spot it?
thanks
Upvotes: 1
Views: 103
Reputation: 549
And, if you guaranty it should returns object then you could use below code:
let jsonDataObject = try! JSONObjectWithData(someData, options:NSJSONReadingOptions())
Here, advantage is we can avoid to use "do { }" scope of syntax
Upvotes: 0
Reputation: 285039
For methods in the Apple frameworks look into the documentation and compare the method signature with its Objective-C equivalent.
In this specific case the Objective-C equivalent is
+ (id)JSONObjectWithData:(NSData *)data
options:(NSJSONReadingOptions)opt
error:(NSError * _Nullable *)error
so the object in the catch
statement is an NSError
object
do {
let jsonData = try JSONObjectWithData(someData, options:NSJSONReadingOptions())
} catch let error as NSError {
print(error)
}
Upvotes: 1