tristanhimmelman/AlamofireObjectMapper

extension protocol Mappable

Closed this issue · 1 comments

I think for a relatively simple model, you can use the attribute list assignment , thus reducing the written part of the code

class TemplateItem:NSObject, Mappable {
    var name: String?
    var picUrl: String?
    var playUrl: String?
    var icon: String?
    var tag: String?
    var desc: String?
    var hotDegree: String?
    var hotType: Int = 0

    required init?(_ map: Map) {

    }

    func mapping(map: Map) {
        self.mapping(map, anyObject: self)
    }
}
extension Mappable {
    func mapping(map: Map, anyObject: AnyObject) {
        var count: UInt32 = 0
        let properties = class_copyPropertyList(anyObject.dynamicType, &count)
        for index in 0..<count {
            let property = properties[Int(index)]
            let cname = property_getName(property)
            if let propertyName = String.fromCString(cname) {
                if let value = map[propertyName].currentValue {
                    anyObject.setValue(value, forKey: propertyName)
                }
            }
        }
        free(properties)
    }
}

Hi @cheyongzi, this is an interesting idea. I will put some thought into and see if I can incorporate it into ObjectMapper.

A side note, this issue should be posted in the ObjectMapper project.