Mapper转换 JSON 为强类型对象

联合创作 · 2023-09-27 14:29

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?'
浏览 6
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报