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

Project 12 / challenge 3 - How do you decode an array if you have no custom object? I'm stuck here can someone help me with this please?

Forums > 100 Days of Swift

I'm trying to save user answers by using Codable on project5 I declared the "save()" method with jsonEncoder where I save it, I have an array called "userWords" where I save all user answers basically, I'm not sure what needs to conform to Codable, I know that in project 10 we had a custom class but here we don't have any custom class. "userWords" provides data in each row of a table view right now, I tried to decode that but I'm keep getting 3 error: "Cannot assign value of type 'Optional<_>' to type '[String]'" "Cannot convert value of type '[String]' to expected argument type 'T.Type'" "Generic parameter 'T' could not be inferred".

3      

Can we see some code? Especially your save() function.

3      

Hi @roosterboy sure! I used exactly the same in this project as well, I also want to save the current word with the user entries as well.


    func save() {
        let jsonEncoder = JSONEncoder()

        if let savedWord = try? jsonEncoder.encode(userWords) {
            let defaults = UserDefaults.standard
            defaults.set(savedWord, forKey: "userWords")
        } else {
            print("Failed saving answer.")
        }
    } 

3      

I was reading one of your comments about this, I did the same mistake by trying to conform my ViewController to Codable, I know that allWords = [String]() and userWords = [String]() are providing data to the ViewController / tableView. If I want to save for example the current word do I need a separate method?

3      

Since userWords is an array of Strings, you don't need to encode it as JSON to store it in UserDefaults. String is a type that can be stored in UserDefaults, so an array of String can be stored as well.

    func save() {
        UserDefaults.standard.set(userWords, forKey: "userWords")
    } 

And you can read it out like this:

    func readUserWords() {
        userWords = UserDefaults.standard.stringArray(forKey: "userWords") ?? []
    }

4      

Oh right! That's actually really cool, thank you! I didn't know about that, I was thinking a lot where or what objects need to conform to Codable, where I got stuck. So basically if we have a custom object then we use Codable or NSCoding.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.