该
jsonObject罐
throw的错误,所以把它内
do块,使用
try和
catch出现的任何错误。在Swift 3中
do { let anyObj = try JSONSerialization.jsonObject(with: data) as! [String: Any] let label = anyObj["label"] as! String let value = anyObj["value"] as! Int let uprate = anyObj["uprate"] as! Int let sufix = anyObj["sufix"] as! String let props = Fieldpropertie(label: label, value: value, uprate: uprate, sufix: sufix) // etc.} catch { print("json error: (error.localizedDescription)")}或者,在Swift 4中,您可以通过
struct遵循以下条件来简化代码
Codable:
struct Fieldpropertie: Codable { let label: String let value: Int let uprate: Int let suffix: String}然后
do { let props = try JSonDeprer().depre(Fieldpropertie.self, from: data) // use props here; no manual parsing the properties is needed} catch { print("json error: (error.localizedDescription)")}


