println(act.actId)
时会出现运行时错误。它抛出一个EXC_BAD_ACCESS
错误。
此问题还有其他方法或解决方法吗?
public class Item {
public var id: String
public init(str: String) {
id = str
}
}
public class Account: Item {
public var actId: String
public override init(str: String) {
actId = "antId: \(str)"
super.init(str: str)
}
}
public class Keyword: Item {
public var keywordId: String
public override init(str: String) {
keywordId = "keywordId: \(str)"
super.init(str: str)
}
}
public class Creator<T: Item> {
public func parse(str: String) -> T {
var result: T = T(str: str)
return result
}
}
var a = Creator<Account>()
var act = a.parse("Apple")
println(act.actId)
答案 0 :(得分:0)
在我的情况下,这不能在游乐场中编译。
默认情况下,子类不会继承其超类初始值设定项 - &gt; Apple
您需要将init()
标记为required
我已经改变了它现在可以使用了
public class Item {
public var id: String
public required init(str: String) {
id = str
}
}
public class Account: Item {
public var actId: String
public required init(str: String) {
actId = "antId: \(str)"
super.init(str: str)
}
}
public class Keyword: Item {
public var keywordId: String
public required init(str: String) {
keywordId = "keywordId: \(str)"
super.init(str: str)
}
}
public class Creator<T: Item> {
public func parse(str: String) -> T {
let result: T = T(str: str)
return result
}
}
var a = Creator<Account>()
var act = a.parse("Apple")
print(act.actId)
有关详细信息,请参阅similar question