LoginSignup
3
2

More than 5 years have passed since last update.

Swift Wrap で JSON encode を簡単にまとめてみましょう。

Last updated at Posted at 2016-03-14

さて、 Swift で Json を encode 簡単にできるライブラリはたくさんありますね。
その中で今気に入っているライブラリを紹介したいと思います。

struct User: WrapCustomizable {
   let name: String
   let age: Int
   let isMan: Bool

   func keyForWrappingPropertyNamed(propertyName: String) -> String? {
      if propertyName == "isMan" {
         return "is_man"
      }
      return propertyName
   }
}

のような Model を

{
   "name" : "shun",
   "age : 28,
   "is_man" : true
}

のような Json(Dictionary) に変換します。

具体的には。

let dictionary: [String : AnyObject] = try Wrap(user)

以上になります。
簡単ですね!

もっと使い込むと、すごくありがたみがわかります!
これでも十分ありがたいですがw

是非皆さんも使ってみてください!!

参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2