当前位置 : 主页 > 手机开发 > 其它 >

swift – 使用Alamofire和Objectmapper,整数值始终为零

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在使用Alamofire和ObjectMapper,我的模型类就是这样 class Category: Object, Mappable {dynamic var id: Int = 0dynamic var name = ""dynamic var thumbnail = ""var children = ListCategory()override static func primaryKey() - Str
我正在使用Alamofire和ObjectMapper,我的模型类就是这样

class Category: Object, Mappable {

dynamic var id: Int = 0
dynamic var name = ""
dynamic var thumbnail = ""
var children = List<Category>()


override static func primaryKey() -> String? {
    return "id"
}


required convenience init?(_ map: Map) {
    self.init()
}

func mapping(map: Map) {
    id <- map["id"]
    name <- map["name"]
    thumbnail <- map["thumbnail"]
    children <- map["children"]
}

}

而我正在使用这样的Alamofire

Alamofire.request(.GET, url).responseArray { (response: Response<[Category], NSError>) in

        let categories = response.result.value

        if let categories = categories {
            for category in categories {
                print(category.id)
                print(category.name)
            }
        }
    }

id永远为零,我不知道为什么?

我通过在模型类中的映射函数中添加转换来修复它

id <- (map["id"], TransformOf<Int, String>(fromJSON: { Int($0!) }, toJSON: { $0.map { String($0) } }))

感谢@BobWakefield

网友评论