tristanhimmelman/AlamofireObjectMapper

Fails to map properties with Int/Bool type

Closed this issue · 1 comments

Hi,

I'm having a problem trying to map Int and Boolean values that I received from a JSON source. I can see that the values look info in the response (that they arrive without quotes ""), however, for some reason they are not mapped correctly and does not show up, when I print the objects from Realm - anyone know why this is? It seems to happen for any type other than String? I've even tried doing transformOf on the spceific properties, but to no avail. This is my model:

`
import Foundation
import ObjectMapper
import AlamofireObjectMapper
import RealmSwift

class Course: Object, Mappable {
    var level: String?
    var code: String?
    var name: String?
    var ects: Int?
    var date: String?
    var registered: String?
    var grade = 0
    var ects_grade: String?
    var passed: Bool?
    var examiner: String?
    var grading: String?
    var evaluation: String?
    var old_average: Double?
    var new_average: Double?


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

    func mapping(map: Map) {
        level <- map["level"]
        code <- map["code"]
        name <- map["name"]
        ects <- (map["ects"])
        date <- map["date"]
        grade <- map["grade"]
        registered <- map["registered"]
        ects_grade <- map["ects-grade"]
        examiner <- map["examiner"]
        grading <- map["grading"]
        evaluation <- map["evaluation"]
        passed <- (map["id"], TransformOf<Bool, String>(fromJSON: { Bool($0!) }, toJSON: { $0.map { String($0) } }))
    }
}

`

As far as I know, you cannot initialize a Bool with a string variable as you are in the transform (Bool($0!)). My suspicion is that you will need to write another function for converting a string to a boolean type. Hope this helps.