我仅在NSObject类上遇到这些错误。 UIButton和UILabel没有此类错误。
class Foo: NSObject, NSCoding {
var title = ""
var children: [Foo] = []
// MARK: NSCoding
override public func encode(with coder: NSCoder) {
super.encode(with: coder)
coder.encode(title as Any?, forKey: "title")
coder.encode(children as Any?, forKey: "children")
}
required public init?(coder decoder: NSCoder) {
super.init(coder: decoder)
self.title = decoder.decodeObject(forKey: "title") as? String ?? ""
self.children = decoder.decodeObject(forKey: "children") as? [Foo] ?? []
}
}
有什么想法吗?
NSCoding(和Codable)是一个协议,因此您不会覆盖任何内容,因此请删除override
和对super
的所有调用
public func encode(with coder: NSCoder) {
coder.encode(title as Any?, forKey: "title")
coder.encode(children as Any?, forKey: "children")
}
required public init?(coder decoder: NSCoder) {
self.title = decoder.decodeObject(forKey: "title") as? String ?? ""
self.children = decoder.decodeObject(forKey: "children") as? [Foo] ?? []
}