Ios8yahoo swift-json

15
Swift-JSON @dankogai

description

A brief introduction of https://github.com/dankogai/swift-json

Transcript of Ios8yahoo swift-json

Page 1: Ios8yahoo swift-json

Swift-JSON@dankogai

Page 2: Ios8yahoo swift-json

What is it?

Page 3: Ios8yahoo swift-json

https://github.com/dankogai/swift-json

Page 4: Ios8yahoo swift-json

swift-json is…• Wraps lengthy objective-c NSJSONSerialization

… • into concise, swifty JSON • even swiftier than SwiftyJSON  • Works both in Swift 1.0 (Xcode 6.0) and Swift 1.1

Page 5: Ios8yahoo swift-json

Object->JSONlet obj:[String:AnyObject] = [ "array": [JSON.null, false, 0, "", [], [:]], "object":[ "null": JSON.null, "bool": true, "int": 42, "double": 3.141592653589793, "string": "a α\t弾\n𪚲", "array": [], "object": [:] ], "url":"http://blog.livedoor.com/dankogai/" ]

Page 6: Ios8yahoo swift-json

Object->JSONlet json = JSON(obj)

Page 7: Ios8yahoo swift-json

String->JSONlet json = JSON( string:"{\"url\":\"http://blog.livedoor.com/dankogai/\",\"array\":[null,false,0,\"\",[],{}],\"object\":{\"object\":{},\"bool\":true,\"null\":null,\"array\":[],\"int\":42,\"double\":3.141592653589793,\"string\":\"a α\t弾\n𪚲\"}}"

)

Page 8: Ios8yahoo swift-json

URL->JSONlet json = JSON( URL:"http://api.dan.co.jp/asin/4534045220.json"

)

Page 9: Ios8yahoo swift-json

Tree Traversaljson["object"]["null"].asNull // NSNull json["object"]["bool"].asBool // true json["object"]["int"].asInt // 42 json["object"]["double"].asDouble // 3.141592653589793 json["object"]["string"].asString // "a α\t弾\n𪚲"

json["array"][0].asNull // NSNull() json["array"][1].asBool // false json["array"][2].asInt // 0 json["array"][3].asString // ""

Page 10: Ios8yahoo swift-json

Error Handlingif let b = json["noexistent"][1234567890]["entry"].asBool { // .... } else { let e = json["noexistent"][1234567890]["entry"].asError println(e) } // Error Domain=JSONErrorDomain Code=404 "["noexistent"] not found" UserInfo=0x10064bfc0 {NSLocalizedDescription=["noexistent"] not found}

Page 11: Ios8yahoo swift-json

Type Checkingjson["object"]["null"].type // "NSNull" json["object"]["null"].isNull // true json["object"]["bool"].type // "Bool" json["object"]["bool"].isBool // true json["object"]["bool"].isNumber // false json["object"]["int"].type // "Int" json["object"]["int"].isInt // 42 json["object"]["int"].isNumber // true json["object"]["double"].type // "Double" json["object"]["double"].isDouble // true json["object"]["double"].isNumber // true json["object"]["string"].type // "String" json["object"]["string"].isString // true

Page 12: Ios8yahoo swift-json

Schema as Classclass MyJSON : JSON { override init(_ obj:AnyObject){ super.init(obj) } override init(_ json:JSON) { super.init(json) } var null :NSNull? { return self["null"].asNull } var bool :Bool? { return self["bool"].asBool } var int :Int? { return self["int"].asInt } var double:Double? { return self["double"].asDouble } var string:String? { return self["string"].asString } var url: String? { return self["url"].asString } var array :MyJSON { return MyJSON(self["array"]) } var object:MyJSON { return MyJSON(self["object"]) } }

Page 13: Ios8yahoo swift-json

Schema as Classlet myjson = MyJSON(obj) myjson.object.null // NSNull? myjson.object.bool // Bool? myjson.object.int // Int? myjson.object.double // Double? myjson.object.string // String? myjson.url // String?

Page 14: Ios8yahoo swift-json

One More Thing

• Software Design で次号から連載します

Page 15: Ios8yahoo swift-json

you.thank!for q in questions { q.answer }