NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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?


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


...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!


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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.