tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.16k stars 1.03k forks source link

Can I parse Json object with initial values? #1098

Open EmreKayaoglu opened 4 years ago

EmreKayaoglu commented 4 years ago

Your model:

class Project: Mappable {
    var id: String?
    var projectName: String?
    var projectDescription: String?
    var companyId: String?
    var isService: Bool = false

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

    func mapping(map: Map) {
        id                          <- map["_id"]
        projectName                 <- map["projectName"]
        projectDescription          <- map["projectDescription"]
        if isService {
            companyId                   <- map["serviceId"]
        } else {
            companyId                   <- map["companyId"]
        }
    }
}

What I want

Currently, I can get Project object as following

let project = Project(JSON: item)

Can I get Project object with initial values like following?

let project = Project(JSON: item, isService: false)