Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ios8yahoo swift-json

23,534 views

Published on

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

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Ios8yahoo swift-json

  1. 1. Swift-JSON @dankogai
  2. 2. What is it?
  3. 3. https://github.com/dankogai/swift-json
  4. 4. 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
  5. 5. Object->JSON let 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/" ]
  6. 6. Object->JSON let json = JSON(obj)
  7. 7. String->JSON let 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ꚲ"}}" )
  8. 8. URL->JSON let json = JSON( URL:"http://api.dan.co.jp/asin/4534045220.json" )
  9. 9. Tree Traversal json["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 // ""
  10. 10. Error Handling if 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}
  11. 11. Type Checking json["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
  12. 12. Schema as Class class 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"]) } }
  13. 13. Schema as Class let 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?
  14. 14. One More Thing • Software Design で次号から連 載します
  15. 15. you.thank! for q in questions { q.answer }

×