SmartCodable is a data parsing library based on Codable. It is simple to use, with robust compatibility being one of its main features. SmartCodable 是基于Codable实现的数据解析库。简单易用,强悍的兼容性是SmartCodable的主要特点。 表层API和功能几乎和HandyJSON一致,支持快速的迁移。
final class SAKURA_AnchorModel: SmartCodable,Hashable,TableCodable {
required init() {}
var id = 0
var is_nsfw:Bool = false
@SmartAny
var profile:SAKURA_ProfileModel = SAKURA_ProfileModel()
@SmartAny
var chat_cover_obj:SAKURA_CoverObjModel?
var media_count = 0
var need_vip:Bool = false
var tags:[[String: String]] = []
var medias:[[String: String]] = []
var is_online:Bool = false
var is_new:Bool = false
var role: Int = 0
var follower_count: Int = 0
var is_busy:Bool = false
var is_hot:Bool = false
//profile
var gender = 0
var profileId = 0
var birthday = ""
@SmartAny
var attrs:Any?
@SmartAny
var location:Any?
var nickname = ""
var age = 0
var head = ""
var cover = ""
var mood:String?
@SmartAny
var settings:Any?
static func == (lhs:SAKURA_AnchorModel,rhs:SAKURA_AnchorModel) -> Bool{
return lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
enum CodingKeys: String, CodingTableKey {
typealias Root = SAKURA_AnchorModel
////列出应绑定到表的属性
case id
case role
case is_hot
case is_busy
case follower_count
case is_new
case is_online
case need_vip
case media_count
case is_nsfw
//profil
case gender
case profileId
case birthday
case nickname
case age
case head
case cover
case mood
case tags
case medias
//主键、唯一、不为null、默认值等的列约束。它是可选的。
static let objectRelationalMapping = TableBinding(CodingKeys.self) {
BindColumnConstraint(id, isPrimary: true)
}
}
}
class SAKURA_AnchorListResponse:SmartCodable {
required init() {
//
}
var anchors:[SAKURA_AnchorModel]?
var tokens:[SAKURA_AnchorModel]?
}
class SAKURA_CoverObjModel:SmartCodable {
required init() {
//
}
var id: String?
var thumb: String?
var mime: String?
var m: String?
var l: String?
}
当我使用SmartCodable构建模型并绑定属性到表时发现,自定义模型的解码并不生效,以下代码model都转换失败为nil,当注去除数据库协议TableCodable,解析正常,两个库之间有协议冲突? import UIKit import SmartCodable import WCDBSwift
final class SAKURA_AnchorModel: SmartCodable,Hashable,TableCodable {
}
class SAKURA_AnchorListResponse:SmartCodable { required init() { // } var anchors:[SAKURA_AnchorModel]? var tokens:[SAKURA_AnchorModel]? }
class SAKURA_CoverObjModel:SmartCodable { required init() { // } var id: String? var thumb: String? var mime: String? var m: String? var l: String? }