Created
March 1, 2015 17:04
-
-
Save josephlord/cc6824401a747902810e to your computer and use it in GitHub Desktop.
A very minimal wrapper round NSJSONSerialization.JSONObjectWithData returning a simple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum JSONParseResult { | |
case Success(AnyObject) | |
case Error(NSError) | |
} | |
/// Parses UTF-8 data to the appropriate object. | |
func parseJSON(data:NSData)->(JSONParseResult) { | |
var err:NSError? | |
let parsed:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &err) | |
if let err = err { | |
return .Error(err) | |
} else if let parsed:AnyObject = parsed { | |
return .Success(parsed) | |
} | |
fatalError("Expected either a parse result or an error") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment