我正在使用Alamofire和ObjectMapper,我的模型类就是这样 class Category: Object, Mappable {dynamic var id: Int = 0dynamic var name = ""dynamic var thumbnail = ""var children = ListCategory()override static func primaryKey() - Str
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