Checklists error: let data = try encoder.encode(items)

iOS Apprentice v6.0, Pages: 328-329
Inside method: saveChecklistItems()
For the line: “let data = try encoder.encode(items)
I get the following compile error: “Generic parameter ‘Value’ could not be inferred
…and the little red underscore is under the “e” of “encode()

SOLUTION: I dunno… I did a few cleans and a few builds and it would go away and come back, but it seems to be gone now.

BTW: I’m using final Xcode 9.3

2 Likes

If you had originally worked on the project using Xcode 9.2 and then switched to 9.3, then it is possible that some previous compilation artifact was causing issues. Cleaning the project and quitting Xcode and restarting are good things to try when this happens.

Since it appears to be working fine for you now, I’ll assume that all is good.

Actually, I started project in Xcode 9.3, but yeah, everything is inexplicably fine after a few cleans and builds.

Thanks for your reply though. :slight_smile:

I had this issue in Xcode 9.3 (9E145) and cleans/builds/restarts would not fix the issue. I decided to move on and a few pages later, iOS Apprentice addresses the issue for the app crashing by adding Codable to the declaration of the ChecklistItem Class. Perhaps the compiler now catches this error.

1 Like

It’s hard to tell what might be going on with just the snippet of code :slight_smile: If you can ZIP up your project showing the issue and upload it somewhere and provide a link, I can take a look to see what might be going on - provided that I can replicate the issue at this end with your project …

hi Fahim, Sorry ))) I was wrong.

I’m running into this same exact problem following along in the book. Here’s my code snippet. The goal is to create an encoder so you can write your items (an array of ChecklistItems) to a plist file. ChecklistItem now has NSObject in the class definition as well:

func saveChecklistItems() {
        let encoder = PropertyListEncoder()
        do {
            let data = try encoder.encode(items)
            try data.write(to: dataFilePath(), options: Data.WritingOptions.atomic)
        } catch {
            print("Error encoding item array!")
        }
    }

as another poster suggested, adding Codable to the class definition for ChecklistItem fixed the build error. Is it possible the compiler got smarter? The book says if you run the app at that point, it’ll crash anyway - so maybe it’s just preventing that? :slight_smile:

1 Like

Actually, this looks like an issue of people compiling the code without reading on :slight_smile: Basically, if you read forward to page 331, it does explain there that the code you’ve added so far will not work properly at that point. The only difference is that the error that used to be thrown has changed from a runtime one to a compiler time one and so, as you say, the compiler probably did get smarter in between when the book was written (which was with a pre-release version of Xcode 9) and now (where Xcode is sitting at version 9.3.x) …

Just to clarify for others who might encounter this issue and read this thread, yes, the error message provided in the book and the one you see when compiling might be different. But if you add support for the Codable protocol as per page 331 and then compile, you should not be running into issues.

1 Like

TL;DR: The author explains how to silence this warning/fix the error on page 332
Modify: class ChecklistItem: NSObject, Codable { so that is conforms the the Codable Protocol

The compiler complains right as you start to type the next line.
let data = try encoder.encode(items) causes the compiler warning: "Generic parameter 'Value' could not be inferred"

Yes, it is now a compiler time one.

My frustration was that the compiler warning caused the constant data to not have a type (it should be Data).
let data: <<error type>>
Therefore Xcode’s incredible autocomplete feature was unable to show the methods available to a Data type.
try data.write(to: dataFilePath(), options: Data.WritingOptions.atomic)

Not a big issue I know, but for someone who relies on reading the Description provided when learning a new method/type/concept/etc as I go this was frustrating.

Worry not, the author explains how to fix this error on page 332
which is to modify: class ChecklistItem: NSObject, Codable {


Leaving this here for searches via chapter:
iOS Apprentice Chapter 15: Saving and Loading

adding cadable fix it

This topic was automatically closed after 166 days. New replies are no longer allowed.