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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.