Swift 2016-04-11 11:10
Swift JSON 解析(对象映射)库 Decodable
回复:0 查看:3899 感兴趣:9 赞:0
Swift JSON 解析(对象映射)库 Decodable。
示例代码:
struct Repository { let name: String let description: String let stargazersCount: Int let language: String? let sometimesMissingKey: String? let owner: User // Struct conforming to Decodable let defaultBranch: Branch // Struct NOT conforming to Decodable var fullName: String { return "\(owner.login)/\(name)" } } extension Repository: Decodable { static func decode(j: AnyObject) throws -> Repository { return try Repository( name: j => "name", description: j => "description", stargazersCount: j => "stargazers_count", language: j => "language", sometimesMissingKey: j =>? "sometimesMissingKey", owner: j => "owner", defaultBranch: Branch(name: j => "default_branch") ) } } do { let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) let repo = try Repository.decode(json) } catch { print(error) }
相关开源代码
EFIconFont: 一个用 Swift 实现的 IconFont 封装
Swift Zip文件压缩与解压框架
EFResume - 一个普通的 Swift 简历模板
Swift 优雅的定时器及执行工具库 Each
Swift 解析、格式化及验证国际电话号码工具库 PhoneNumberKit
Swift 版 FlatBuffers(跨平台序列化库)FlatBuffersSwift
Swift 编写的 Github API 客户端 Github.swift
Swift OAuth授权库 SwiftyOAuth
Swift 音频处理库 TempiFFT
iOS通知中心封装库SwiftNotificationCenter