【发布时间】:2017-01-07 18:50:47
【问题描述】:
我尝试将以下代码放入 Swift 3 中。super.encodeWithCoder(aCoder) 行出现问题。无论我做什么都会出错。
import Foundation
class ToDo: Task {
var done: Bool
@objc required init(coder aDecoder: NSCoder) {
self.done = aDecoder.decodeObjectForKey("done") as! Bool
super.init(coder: aDecoder)
}
@objc override func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(done, forKey: "done")
super.encodeWithCoder(aCoder)
}
init(name: String, done: Bool) {
self.done = done
super.init(name: name)
}
}
我正在尝试转换为 Swift 3
我有这个
import Foundation
class ToDo: Task {
var done: Bool
@objc required init(coder aDecoder: NSCoder) {
self.done = aDecoder.decodeObject(forKey: "done") as! Bool
super.init(coder: aDecoder)
}
@objc override func encode(with aCoder: NSCoder) {
aCoder.encode(done, forKey: "done")
// THis line gives an error
super.encode(with aCoder)
}
init(name: String, done: Bool) {
self.done = done
super.init(name: name)
}
}
线
super.encodeWithCoder(aCoder)
给出一个错误。 Swift 没有提示,搜索也没有给出答案。
编辑以响应 cmets 原代码“super.encodeWithCoder(aCoder)”给出错误Value of type 'Task' has no member 'encodeWithCoder'
super.encode(with aCoder) 给出错误 Expected ';'分隔符
【问题讨论】:
-
错误是什么?
-
虽然奇怪的编码,这可能是因为我不熟悉覆盖它 - 我一直认为只有在 IB 中创建的视图才使用 init(coder:)。也就是说,我突然想到了两件事:(1)您没有发布您遇到的错误类型的详细信息。 (2) 我可能会看到构建错误,因为您在调用 super.ecode(with: coder) 之前有一些代码。奖励:我刚刚看到您的代码也缺少该行中的冒号。我很确定这肯定不会建立。
-
“
super.encodeWithCoder(aCoder)行出现错误”。您的 Swift 3 示例中不存在该行。您的意思是您对super.encode(with aCoder)的调用无法编译吗?正如 dfd 指出的那样,这不会编译,因为它必须是super.encode(with: aCoder)。