gpt4 book ai didi

swift - ObjectMapper - 在子类中初始化一个空的 init

转载 作者:行者123 更新时间:2023-11-28 13:25:07 26 4
gpt4 key购买 nike

我有一个名为“Base”的基类,它实现了 Mappable 协议(protocol)。我已经按如下所述对该类进行了子类化。

class Base: Mappable {
var base: String?

required init?(map: Map) {

}

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

class Subclass: Base {
var sub: String?

required init?(map: Map) {
super.init(map: map))
}

override func mapping(map: Map) {
super.mapping(map: map)

sub <- map["sub"]
}
}

是否可以在子类中创建一个空的 init,我如何创建这样的子类实例?

var obj = Subclass()

最佳答案

您可以添加 init()在你的里面 Baseoverride init()在你的里面 SubClass

class Base: Mappable {
var base: String?

init() {}

required init?(map: Map) {

}

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

class Subclass: Base {
var sub: String?

override init() {
super.init()
}

required init?(map: Map) {
fatalError("init(map:) has not been implemented")
}

override func mapping(map: Map) {
super.mapping(map: map)

sub <- map["sub"]
}
}

关于swift - ObjectMapper - 在子类中初始化一个空的 init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58500193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com