转换 JSON 为强类型对象:Mapper

jopen 8年前

Mapper 是一个简单的 Swift 库,用于转换 JSON 为强类型对象。和其它库相比,Mapper 的一个优点是你可以有不可改变的特性。

代码:

import Mapper  // Conform to the Mappable protocol  struct User: Mappable {    let id: String    let photoURL: NSURL?    // Implement this initializer    init(map: Mapper) throws {      try id = map.from("id")      photoURL = map.optionalFrom("avatar_url")    }  }  // Create a user!  let JSON: NSDictionary = ...  let user = User.from(JSON) // This is a 'User?'

项目主页:http://www.open-open.com/lib/view/home/1449151754842