美文网首页
Swift 之正确使用 YYModel

Swift 之正确使用 YYModel

作者: willokyes | 来源:发表于2018-01-12 16:50 被阅读0次

使用 Swift 创建 Model 类时,model 类属性变量前需加 @objc ,否则 YYModel 将不能读取 model 类属性变量,即无法解析返回空。


import UIKit

class WBStatus: NSObject {
    //
    @objc var id: Int64 = 0
    @objc var text: String?
    
    //
    override var description: String {
        return yy_modelDescription()
    }
}


class WBStatusListViewModel {
    //
    lazy var statusList = [WBStatus]()
    
    func loadStatus(completion: @escaping (Bool)->()) {
        //
        WBNetworkManager.shared.statusList { (list, isSuccess) in
            guard let array = NSArray.yy_modelArray(with: WBStatus.self, json: list ?? []) as? [WBStatus] else {
                completion(isSuccess)
                return
            }
            
            self.statusList += array
            
            completion(isSuccess)
        }
    }
}

相关文章

网友评论

      本文标题:Swift 之正确使用 YYModel

      本文链接:https://www.haomeiwen.com/subject/bmvfoxtx.html