Skip to content

Instantly share code, notes, and snippets.

@mwrites
Created February 15, 2017 06:21
Show Gist options
  • Save mwrites/5ec473243954df2bdea72052f85480ce to your computer and use it in GitHub Desktop.
Save mwrites/5ec473243954df2bdea72052f85480ce to your computer and use it in GitHub Desktop.
JsonDic parsing helper
struct JsonDic {
let dic : [String:AnyObject]
func integer(key: String) throws -> Int {
guard let s = dic[key] as? Int else { throw ResourceError.parseWrongType(key) }
return s
}
func string(key: String) throws -> String {
guard let s = dic[key] as? String else { throw ResourceError.parseWrongType(key) }
return s
}
func dictionary(key: String) throws -> JsonDic {
guard let d = dic[key] as? JsonDic else { throw ResourceError.parseWrongType(key) }
return d
}
}
enum ResourceError : Swift.Error {
case parseType(String)
case parseWrongType(String)
case parseClosure
case jsonParsing
case serverErrorCode(Int)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment