20:swift-内嵌类型:数据模型
正文
/*内嵌类型: 定义数据模型 1: 枚举通常用于实现特定类或结构体的功能。 类似的,它也可以在更加复杂的类型环境中方便的定义通用类和结构体。 为实现这种功能,Swift 允许你定义内嵌类型,借此在支持类型的定义中嵌套枚举、类、或结构体。 若要在一种类型中嵌套另一种类型,在其支持类型的大括号内定义即可。可以根据需求多级嵌套数个类型。 */ import UIKit struct BDTGiftModel: Codable { var errno: String var errmsg: String // 重写解析方法 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) errmsg = try container.decode(String.self, forKey: .errmsg) // 兼容todoDaysCnt字段 do { errno = try container.decodeIfPresent(String.self, forKey: .errno) ?? "0" } catch DecodingError.typeMismatch { if let errnoInt = try container.decodeIfPresent(Int.self, forKey: .errno) { errno = String(errnoInt) } else { errno = "0" } } } } class InsertType20VC: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } }