quattro_4 scribble

scribble 落書き (調べた事をただ落書きする)

NSScreencast Episode #129 Swift JSON

Swift JSON - NSScreencast

SwiftJSONをパース

  • Obj-Cの方がSwiftよりJSONパースが適当に実装できる
    • parsing JSON (which provides no contracts or type guarantees) can be difficult and tedious in Swift.
    • Many of the problems you are forced to deal with were easier to ignore in Objective-C
  • func parseJson
    • var error: NSError?
    • let json: AnyObject = NSJSONSerialization.JSONObjectWithData
    • if error {
    • else
  • struct Episode
  • func parseEpisodes(json: AnyObject) -> [Episode]
    • if let array = json as? [ [ String : AnyObject ] ]
  • func parseEpisode(dict: [ String : AnyObject ]) -> Episode?
    • if let id = dict["id"] as? NSNumber {
      • if let title = dict["title"] as? NSString {
        • if let description = dict["description"] as? NSString {
          • if let number = dict["episode_number"] as? NSNumber {

自分で毎回いちいち書くとしたら面倒そう