jackdm
jackdm

Reputation: 329

Multiple methods to init object?

I have a model which uses init with JSON, so that i can create objects directly from an API response.

However in this instance I want to init the model and manually enter its properties, hence it wont be JSON.

How can i have another init method that allows me to not use the standard JSON method and instead manually enter my params?

The model looks like this...

class Conversation: NSObject {

    var id: String
    var index: String
    var image: String
    var firstname: String
    var lastname: String
    var withuserid: String
    var badgeCount: String

    init?(_ json: JSON) {
        guard let id = json["id"].string,
            let index = json["index"].string,
            let image = json["image"].string,
            let firstname = json["firstname"].string,
            let lastname = json["lastname"].string,
            let withuserid = json["withuserid"].string,
            let badgeCount = json["badgeCount"].string

            else { return nil }

        self.id = id
        self.index = index
        self.image = image
        self.firstname = firstname
        self.lastname = lastname
        self.withuserid = withuserid
        self.badgeCount = badgeCount
    }
}

Upvotes: 0

Views: 111

Answers (2)

David Pasztor
David Pasztor

Reputation: 54706

A single class or struct can have several designated initializers in Swift.

Then you can call the non-JSON init method like this:

let conv = Conversation(id: "asbe", index: "1", "image: "img", firstName: "John", lastName: "Smith", withUserId: "21", badgeCount: "5"

class Conversation {

    var id: String
    var index: String
    var image: String
    var firstName: String
    var lastName: String
    var withUserId: String
    var badgeCount: String

    init(id: String, index: String, image: String, firstName: String, lastName: String, withUserId: String, badgeCount: String){
        self.id = id
        self.index = index
        self.image = image
        self.firstName = firstName
        self.lastName = lastName
        self.withUserId = withUserId
        self.badgeCount = badgeCount
    }

    init?(_ json: JSON) {
        guard let id = json["id"].string,
            let index = json["index"].string,
            let image = json["image"].string,
            let firstname = json["firstname"].string,
            let lastname = json["lastname"].string,
            let withuserid = json["withuserid"].string,
            let badgeCount = json["badgeCount"].string

            else { return nil }

        self.id = id
        self.index = index
        self.image = image
        self.firstname = firstname
        self.lastname = lastname
        self.withuserid = withuserid
        self.badgeCount = badgeCount
    }
}

Some general advice: I don't see any reason for the class to inherit from NSObject. Swift classes don't need to inherit from any class, so unless you explicitly need a method from another class, don't make your custom classes inherit from others for no reason.

Please make sure you conform to the Swift naming convention, which is lower-camelCase for variable names. I would also rethink the types of some of the variables, without more context, it seems to me, some of them should be of type Int instead of String.

Upvotes: 1

Rashwan L
Rashwan L

Reputation: 38833

Just add another init since you can several ones for a struct and class Then just call the desired one:

class Conversation: NSObject {
    var id: String
    var index: String
    var image: String
    var firstname: String
    var lastname: String
    var withuserid: String
    var badgeCount: String

    init?(_ json: JSON) {
        guard let id = json["id"].string,
            let index = json["index"].string,
            let image = json["image"].string,
            let firstname = json["firstname"].string,
            let lastname = json["lastname"].string,
            let withuserid = json["withuserid"].string,
            let badgeCount = json["badgeCount"].string

            else { return nil }

        self.id = id
        self.index = index
        self.image = image
        self.firstname = firstname
        self.lastname = lastname
        self.withuserid = withuserid
        self.badgeCount = badgeCount
    }

    init(id: String, index: String, image: String, firstName: String, lastName: String, withUserId: String, badgeCount: String) {
        self.id = id
        self.index = index
        self.image = image
        self.firstName = firstName
        self.lastName = lastName
        self.withUserId = withUserId
        self.badgeCount = badgeCount
    }
}

Upvotes: 1

Related Questions