Mapper轉(zhuǎn)換 JSON 為強類型對象
Mapper 是一個簡單的 Swift 庫,用于轉(zhuǎn)換 JSON 為強類型對象。和其它庫相比,Mapper 的一個優(yōu)點是你可以有不可改變的特性。
代碼:
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?'評論
圖片
表情
