Tutorial 2: Checklists p. 136-138

Hi, I’m working on the Checklists app and I’m getting these errors;
import Foundation

class ChecklistItem: NSObject, NSCoding {Type ‘ChecklistItem’ does not conform to protocol ‘NSCoding’

var text = ""
var checked = false

func toggleChecked() {
checked = !checked
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(text, forKey: “Text”) ‘encodeObject(:forKey:)’ has been renamed to 'encode(:forKey:)’
aCoder.encodeBool(checked, forKey: “Checked”) ‘encodeBool(:forKey:)’ has been renamed to 'encode(:forKey:)’
}

required init?(coder aDecoder: NSCoder) {
    super.init()
}

override init() {
    super.init()
}

}

This is my code (the bold sentences are the errors I get…)
I have the newer version of XCode, is this the problem?

Thanks in advance!

Yes. Some of the Foundation types and functions have been renamed in Swift 3, and Xcode can frequently tell you what the more up to date names are for the functions you’re using. (You may have to use a bit of judgement to get the right result).

I think as far as we’re concerned, the question is ‘are you using the most up to date version of the book?’, because we may have to make corrections on the book! So if you do spot more things like this let us know.

Yeah, I’ve got the latest version of the book… But do you have an idea how to solve these errors?

You need to edit the method names which have been renamed.
You can’t continue using aCoder.encodeObject(text, forKey: “Text”) Change encodeObject to encode.

As for the NSCoding error, you need to read the book!
You have told ChecklistItem it conforms to the NSCoding protocol, but possibly haven’t provided it with the methods it requires.

I don’t think you have the latest version of the book. The latest version is v5.0 and all the methods have been renamed for Swift 3 in this edition.

If you’re a PDF customer, then go to www.raywenderlich.com/loot to download the latest version.

Thanks!
I changed the removed the Object piece and it worked…

For the NSCoding error I had to add the bold bit…

class ChecklistItem: NSObject, NSCoding {
public func encode(with aCoder: NSCoder) {


** }**

var text = ""
var checked = false

I’ve gotten the latest version (v5.0) of the Checklists tutorial… But I already figured out how to fix it :slight_smile:

Hi,
On Which page did you find the below code?
required init?(coder aDecoder: NSCoder) {
super.init()
}

override init() {
super.init()
}

Regards

Tibor

@noah8610 Do you still have issues with this?

Hi, I just wanted to read about it in the book, but the book version is using Codable protocol, which was introduced in Swift 4. I thought if I download the book which used Swift 3, then I thought I find more info about NSCoder System and I was right :slight_smile: I found satisfying amount of info there.
Anyone needs more info just download the version using Swift 3…
Many thanks!