-
Notifications
You must be signed in to change notification settings - Fork 4
Codable
ShenYj edited this page Mar 11, 2021
·
3 revisions
Codable
协议在Swift4.0
开始被引入,目标是取代现有的NSCoding
协议,它对结构体,枚举和类都支持,能够把JSON
这种弱类型数据转换成代码中使用的强类型数据
import Foundation
let json = """
[
{
"name": "Banana",
"points": 200,
"description": "A banana grown in Ecuador."
},
{
"name": "Orange",
"points": 100
}
]
""".data(using: .utf8)!
struct GroceryProduct: Codable {
var name: String
var points: Int
var description: String?
}
let decoder = JSONDecoder()
let products = try decoder.decode([GroceryProduct].self, from: json)
- 非可选项的
属性
一定要保持一致, 否则就会decode
失败, 返回nil
- 模型中的
属性
相对于json
中的key
只多不少, 多出的属性- 如果是可选项,
decode
成功, 只不过是缺失的属性为nil
- 如果是必选项,
json
中不存在时, 整个json
decode
失败, 返回nil
- 如果是可选项,
import Foundation
let json = """
[
{
"product_name": "Bananas",
"product_cost": 200,
"description": "A banana grown in Ecuador."
},
{
"product_name": "Oranges",
"product_cost": 100,
"description": "A juicy orange."
}
]
""".data(using: .utf8)!
struct GroceryProduct: Codable {
var name: String
var points: Int
var description: String
private enum CodingKeys: String, CodingKey {
case name = "product_name"
case points = "product_cost"
case description
}
}
let decoder = JSONDecoder()
let products = try? decoder.decode([GroceryProduct].self, from: json)
products?.forEach { print("name: \($0.name) points: \($0.points) description: \($0.description)") }
-
CodingKeys
必须是嵌套在声明的struct
中, 而且这个枚举名称固定格式- 比如我自定义个名字
CodingKeyss
, 就会decoding
失败, 返回nil
- 比如我自定义个名字
-
CodingKeys
必须遵守CodingKey
协议- 因为键都是
String
类型,所以需要在CodingKeys
上声明为String enum CodingKeys: String, CodingKey
- 因为键都是
- 即使不打算重新命名所有的键也要在
CodingKeys
中列出所有的键- 如果
CodingKeys
中缺少某个模型属性, 编译器直接报错Type 'GroceryProduct' does not conform to protocol 'Decodable'
- 如果
-
准备数据
let json = """ [ { "name": "Home Town Market", "aisles": [ { "name": "Produce", "shelves": [ { "name": "Discount Produce", "product": { "name": "Banana", "points": 200, "description": "A banana that's perfectly ripe." } } ] } ] }, { "name": "Big City Market", "aisles": [ { "name": "Sale Aisle", "shelves": [ { "name": "Seasonal Sale", "product": { "name": "Chestnuts", "points": 700, "description": "Chestnuts that were roasted over an open fire." } }, { "name": "Last Season's Clearance", "product": { "name": "Pumpkin Seeds", "points": 400, "description": "Seeds harvested from a pumpkin." } } ] } ] } ] """.data(using: .utf8)!
-
示例代码
-
常规解析
struct GroceryStoreService: Decodable { let name: String let aisles: [Aisle] struct Aisle: Decodable { let name: String let shelves: [Shelf] struct Shelf: Decodable { let name: String let product: GroceryStore.Product struct GroceryStore { var name: String var products: [Product] struct Product: Codable { var name: String var points: Int var description: String? } } } } } for (index, store) in serviceStores.enumerated() { print("\(index).\(store.name) is selling:") for product in store.aisles { print("\t\(product.name) (\(product.shelves) shelves)") print("\t\t\(product.name)") } }
-
苹果示例代码
struct GroceryStore { var name: String var products: [Product] struct Product: Codable { var name: String var points: Int var description: String? } } struct GroceryStoreService: Decodable { let name: String let aisles: [Aisle] struct Aisle: Decodable { let name: String let shelves: [Shelf] struct Shelf: Decodable { let name: String let product: GroceryStore.Product } } } extension GroceryStore { init(from service: GroceryStoreService) { name = service.name products = [] for aisle in service.aisles { for shelf in aisle.shelves { products.append(shelf.product) } } } } let decoder = JSONDecoder() let serviceStores = try decoder.decode([GroceryStoreService].self, from: json) let stores = serviceStores.map { GroceryStore(from: $0) } for (index, store) in stores.enumerated() { print("\(index).\(store.name) is selling:") for product in store.products { print("\t\(product.name) (\(product.points) points)") if let description = product.description { print("\t\t\(description)") } } }
-
-
第一段是按照嵌套结构转成模型
-
第二段是摘取有效部分组成一个模型(苹果示例代码)
-
准备数据
let json = """ { "Banana": { "points": 200, "description": "A banana grown in Ecuador." }, "Orange": { "points": 100 } } """.data(using: .utf8)!
这段数据中外层是个字典, 包含了多种水果, 内部同样是个字典 常规思路, 我们可能更需要一个集合, 元素是字典(水果), 然后就可以这样定义模型类
struct Product {
let name: String
let points: Int
let description: String?
}
-
示例代码
struct GroceryStore { struct Product { let name: String let points: Int let description: String? } var products: [Product] init(products: [Product] = []) { self.products = products } } extension GroceryStore: Encodable { struct ProductKey: CodingKey { var stringValue: String init?(stringValue: String) { self.stringValue = stringValue } var intValue: Int? { return nil } init?(intValue: Int) { return nil } static let points = ProductKey(stringValue: "points")! static let description = ProductKey(stringValue: "description")! } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: ProductKey.self) for product in products { // Any product's `name` can be used as a key name. let nameKey = ProductKey(stringValue: product.name)! var productContainer = container.nestedContainer(keyedBy: ProductKey.self, forKey: nameKey) // The rest of the keys use static names defined in `ProductKey`. try productContainer.encode(product.points, forKey: .points) try productContainer.encode(product.description, forKey: .description) } } } extension GroceryStore: Decodable { public init(from decoder: Decoder) throws { var products = [Product]() let container = try decoder.container(keyedBy: ProductKey.self) for key in container.allKeys { // Note how the `key` in the loop above is used immediately to access a nested container. let productContainer = try container.nestedContainer(keyedBy: ProductKey.self, forKey: key) let points = try productContainer.decode(Int.self, forKey: .points) let description = try productContainer.decodeIfPresent(String.self, forKey: .description) // The key is used again here and completes the collapse of the nesting that existed in the JSON representation. let product = Product(name: key.stringValue, points: points, description: description) products.append(product) } self.init(products: products) } } let decoder = JSONDecoder() let decodedStore = try decoder.decode(GroceryStore.self, from: json) print("The store is selling the following products:") decodedStore.products.forEach{ print("\t name:\($0.name) - points:\($0.points) - description:\($0.description ?? "null")") }
ShenYj.github.io - 简书地址 - 返回首页
-
Apple
Common
蓝牙
LBS
音视频
- AVAudioSessionchange_route
- 切换线路
- StreamingKit
- Audio Unit 基础
OC 与 Swift 混编
Object-C
- 代码混淆
- autoreleasepool
- 忽略编译器(clang)警告
- 定时器
- 锁
- RunLoop
- block
- NS_REFINED_FOR_SWIFT
- NS_CLOSED_ENUM
- NS_TYPED_ENUM、NS_STRING_ENUM
- NS_TYPED_EXTENSIBLE_ENUM、NS_EXTENSIBLE_STRING_ENUM
- 关键字nonnull和nullable
- class、objc_getClass和object_getclass方法区别
- isKindOfClass和isMemberOfClass
- 应用程序的加载
- non-lazy classes & lazy classes
- load方法
- initialize方法
- 方法的本质
- 类型编码
- self和super
- 类的内存分析
Swift
- precondition
- 权限控制
- Array常用Api
- String初始化、定义
- String常用Api
- String截取演练
- Set定义、创建
- Set访问和修改
- Dictionary操作
- Dictionary和KeyValuePairs
- Dictionary与String转换
- 常用高阶函数
- enum原始值
- enum关联值
- enum遍历
- 递归enum
- enum内存分配
- 指针
- for循环
- break跳出循环
- 变量名与关键字冲突
- 类的定义
- 类的继承和初始化
- 关键字: final
- 关键字: mutating
- 关键字: lazy
- 修饰类方法的关键字: static
- 关键字: final、dynamic、objc和_dynamicReplacement
- 关键字:@dynamicMemberLookup和@dynamicCallable
- 关键字: propertyWrapper
- 自定义运算符
- 下标: subscript
- 扩展: extension
- 协议: protocol
- 协议和扩展
- 为什么需要泛型
- 泛型函数定义
- 泛型类型
- 泛型的类型约束
- 关联类型
- 为泛型定义要求
- 泛型下标
- 多线程
- Attributes
- 错误处理
- Codable
- DispatchSourceTimer
- Swift 5.x 演练: 更多功能编辑页
- Swift 5.x 类库收集
- 单元测试笔记
- 实例对象内存结构
- 元类型、Type、Self
- frozen
- convention
- Swift(5.3.2)源码编译
- SQLite.Swift类库演练
- Swift 5.5 关键字: async/await
- Swift 5.5 新特性: Continuations
- Swift 5.5 新特性: Actor
- Swift 方法调度
- Swift Mirror
- Swift 关键字: @_silgen_name
- Swift 关键字: @_disfavoredOverload
- swiftmodule
- Swift 5.6 新特性: Type placeholders
- Swift 5.6 新特性: #unavailable
- Swift 5.6 新特性: CodingKeyRepresentable
- Swift 5.6 新特性: existential any
- Swift 5.7 新特性: if-let/guard 语法简化
- Swift 5.7 新特性: Multi-statement closure type inference
- Swift 5.8 新特性: @backDeployed
- Swift 5.9 新特性: if switch expressions
- Swift 6.0 新特性:@preconcurrency
RxSwift
macOS - AppKit
-
iOS Assembly(ARM64)
-
C++
C++ 基础
- cout、cin
- 函数重载
- 默认参数
- extern "C"
- pragma once
- inline function
- const
- Reference
- 汇编
- 类和对象
- 堆空间内存管理
- Constructor
- Destructor
- 成员变量初始化
- 声明与实现分离
- namespace
- 继承
- 访问权限
- 初始化列表
- 多态:虚函数
- 多态:虚函数实现原理
- 多态:虚析构函数
- 多态:纯虚函数
- 多态:抽象类
- 多继承
- static
- static: 单例模式
- const 成员
- 引用类型成员
- 拷贝构造函数
- 调用父类的拷贝构造函数
- 浅拷贝、深拷贝
- 对象型参数和返回值
- 匿名对象
- 隐式构造
- 编译器自动生成的构造函数
- 友元
- 内部类
- 局部类
- 运算符重载
- 模板
- 类型转换
- C++标准
- Lambda
- 异常
- 智能指针
-
Flutter
Dart
Flutter
-
Go
Go 基础
-
Ruby
Ruby 基础
-
React-Native
React-Native
-
工具篇
-
Swift Package Manager
-
自动化
-
TroubleShooting
-
扩展