init对象有多种方法?

时间:2017-09-21 08:51:29

标签: ios swift model initialization

我有一个使用带JSON的init的模型,因此我可以直接从API响应创建对象。

但是在这个例子中我想初始化模型并手动输入它的属性,因此它不会是JSON。

我怎么能有另一个init方法允许我不使用标准JSON方法而是手动输入我的参数?

模型看起来像这样......

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
    }
}

2 个答案:

答案 0 :(得分:1)

只需添加另一个init,因为您可以为structclass添加多个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 } } ,然后只需拨打所需的{/ p>

#include <string>

struct Work {
        std::string author;
        std::string name;
        int year;
};

int main() {
        Work s9 {"Beethoven", "Symphony No. 9 in D minor, Op. 125; Choral", 1824}; // memberwise initialization
        Work currently_playing {s9};    // copy initialization

        return 0;
}

答案 1 :(得分:1)

单个类或结构可以在Swift中有几个指定的初始值设定项。

然后你可以像这样调用非JSON init方法:

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
    }
}

一些一般建议:我没有看到该课程继承NSObject的任何理由。 Swift类不需要从任何类继承,因此除非您明确需要来自其他类的方法,否则不要让您的自定义类继承其他类。

请确保符合Swift命名约定,该约定是变量名称的较低camelCase。我也会重新考虑一些变量的类型,没有更多的上下文,在我看来,其中一些应该是Int类型而不是String