I have the following code which I'm trying to put into Swift 3. The line super.encodeWithCoder(aCoder)
is giving problems. Whatever I do gives an error.
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)
}
}
I'm trying to convert to Swift 3
I have this
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)
}
}
The line
super.encodeWithCoder(aCoder)
gives an error . Swift gives no prompt and a search has given no answers.
Edit in response to comments The original code "super.encodeWithCoder(aCoder)" gives the error Value of type 'Task' has no member 'encodeWithCoder'
super.encode(with aCoder) gives error Expected ';' separator
I think the reason that the app crashes is that you use a
decodeObject()
function for a Bool.Changing the code to the correct functions yields: