NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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 Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.