Last active
June 15, 2016 12:07
-
-
Save AlexanderNey/adb42e2947afc14a1bf4 to your computer and use it in GitHub Desktop.
ARTICLE Function Composing Swift 2.0 - 3
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
// Function might throw errors like this: | |
func parseJSONFromNSData(data: NSData) throws -> JSON { | |
// Try to parse but fails ... | |
throw JSONParseError.InvalideJSON | |
} | |
func handleResponse<T : Unmarshallable>(data: NSDate) -> T { | |
let data = ("{'somejson':true'}" as NSString).dataUsingEncoding(NSUTF8StringEncoding)! | |
do { | |
let json = try parseJSONFromNSData(data) | |
let validatedJSON = try validateJSONResponse(json) | |
let model = try unmarshalJSON(validatedJSON, type: Model.self) | |
return model | |
} catch JSONParseError.InvalideJSON { | |
print("Your JSON looks unreal") | |
} catch ServerResponseError.ErrorResponse { | |
print("Server didn't like your request") | |
} catch let error { | |
print("Seriosly unknown error: \(error)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment