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

[Solved] How can I use UserDefaults here, kindly guide

Forums > SwiftUI

I was using this code prior to using UserDefaults , all worked well -

class Songs: ObservableObject {
    @Published var songs: [Song]

    init(){
           self.songs =
            [
                Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),                
                 Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
                Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
                 Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
                 Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
            ]

       }

}

Now i wanted to save the data permanently , but as the data was trivial i did not use core data , insead went for UserDefaults and used the code below, and now the code does not show the data in list etc, how can i make this work with UserDefaults, thanks

class Songs: ObservableObject {

               var songsData =
    [
                 Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),                
                 Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
                Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
                 Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
                 Song(name: "demo", album: "demo", genre: "demo", artist: "demo"),
                ]

    @Published var songs: [Song] {
        didSet {
            UserDefaults.standard.set(songsData, forKey: "demosongs")
        }

    }

    init(){

        self.songs = UserDefaults.standard.object(forKey: "demosongs") as? [Song] ?? [Song.default]
       }

}

1      

@Obelix - hii, yeah a great lesson, my problem seems to stem from trying to save array and not knowing where is the best place to feed the value to UserDefaults, i just tried at onappear etc , but they seem to throw runtime errors, is the below code write way , as the UserDefaults is not getting the values , thanks

@Published var songs: [Song] {
        didSet {
            UserDefaults.standard.set(songsData, forKey: "demosongs")
        }

    }

1      

had to do this -

@Published var songs = [Song]() {
        didSet {
            if let encoded = try? JSONEncoder().encode(songs) {
                UserDefaults.standard.set(encoded, forKey: "demosongs")
            }
        }
    }

and then in init

 if let savedItems = UserDefaults.standard.data(forKey: "demosongs") {
               if let decodedItems = try? JSONDecoder().decode([Song].self, from: savedItems) {
                   songs = decodedItems
                   return
               }

           }
           songs = songsData

1      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.