UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Cannot convert value of type "" to expected argument type 'Data'

Forums > Swift

So I've been trying to write my own small app starting from the basics of one of the existing projects. Specifically, I'm doing something for recording event occurrences and displaying them as a table, using Codable to store the records.

Per Project UIKit Project 12's example, I set up a basic EventRecord class and conformed it to Codable, as follows:

class EventRecord: NSObject, Codable {
    var eventDescription: String
    var occurrences: Int
    var notes: String

    init(eventDescription: String, occurrences: Int, notes: String) {
        self.eventDescription = eventDescription
        self.occurrences = occurrences
        self.notes = notes
    }
}

In the main TableViewController I've got the following included at the end of viewDidLoad():

        let defaults = UserDefaults.standard

        if let savedEntries = defaults.object(forKey: "entries") as? EventRecord {
            let jsonDecoder = JSONDecoder()

            do {
                entries = try jsonDecoder.decode([EventRecord].self, from: savedEntries)
            } catch {
                print("Failed to load entries")
            }
        }

The try line is being flagged with a fatal error, reading as follows: "Cannot convert value of type 'EventRecord' to expected argument type 'Data'".

I've spent over 4 hours tinkering with the code, comparing against the example in the book to see if I missed anything, and looking up additional information online, but I can't figure out what I'm doing that's any different from the example, which worked when I was working through it previously. All of the information is in basic types except for the conformed class itself, and when compared to the original sample code the only thing that's really different is the class name. So why isn't it working?

2      

savedEntries is an EventRecord because that's how you typecasted it in the if let statement. So when jsonDecoder.decode is looking for a parameter of type Data you are giving it an EventRecord instead

3      

...went back to check the example in the book again, and sure enough it's sayind as Data there, too. Skimming the rest of the project again, it appears that what was confusing me was this particular line from the introduction to UserDefaults (just before it introduces the NSCoding method) remained stuck in my head as I was debugging things:

let dict = defaults.object(forKey: "SavedDict") as? [String: String] ?? [String: String]()

Thanks again!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.