Skip to content

Instantly share code, notes, and snippets.

@cojoj
Last active August 29, 2015 14:22
Show Gist options
  • Save cojoj/0eed4e67848d3f592310 to your computer and use it in GitHub Desktop.
Save cojoj/0eed4e67848d3f592310 to your computer and use it in GitHub Desktop.
ErrorType vs NSError
public class func parse(data: NSData) -> (AnyObject?, NSError?) {
do {
// From docs: If an error occurs, upon return contains an NSError object that describes the problem.
let obj = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
return (obj as AnyObject, nil)
} catch {
// But here I get: ErrorType is not convertible to NSError
return (nil, error)
}
}
@rad3ks
Copy link

rad3ks commented Jun 12, 2015

return (nil, error as NSError) thanks to ErrorType bridging to NSError. You can also catch let error as NSError because catch here is a shorthand for catch let error. Docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment