Objectmapper: Singleton Design with ObjectMapper

Created on 28 Nov 2016  路  6Comments  路  Source: tristanhimmelman/ObjectMapper

Hi! I've design my models that implements singleton patterns with sharedInstance.

class Car {
    static let sharedInstance = Car()
}

How can I implement singleton pattern using sharedInstance in ObjectMapper class? When I'm working with sharedInstance and Mappable. This cause a error like _Missing argument for parameter 'map' in call_

class Car: Mappable {

    static let sharedInstance = Car()

    var mode: String?

    required init?(map: Map) { }

    func mapping(map: Map) {
        mode    <- map["info"]["mode"]
    }
}

Most helpful comment

Just add an initializer:

class Car: Mappable {

    static let sharedInstance = Car()

    var mode: String?

+   init() {
+       self.mode = nil
+   }

    required init?(map: Map) { }

    func mapping(map: Map) {
        mode    <- map["info"]["mode"]
    }
}

All 6 comments

Just add an initializer:

class Car: Mappable {

    static let sharedInstance = Car()

    var mode: String?

+   init() {
+       self.mode = nil
+   }

    required init?(map: Map) { }

    func mapping(map: Map) {
        mode    <- map["info"]["mode"]
    }
}

Another alternative would be to look into the StaticMappable protocol (see README)

Thank you so much @tristanhimmelman and @devxoul

@anddygon what does that mean?

@anddygon what do you want?

Adding an empty initializer solves the problem.

init() {

 }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

borut-t picture borut-t  路  4Comments

nearspears picture nearspears  路  4Comments

AashishSapkota picture AashishSapkota  路  3Comments

zhengying picture zhengying  路  4Comments

VictorAlbertos picture VictorAlbertos  路  3Comments