NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

[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]
       }

}

   

@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")
        }

    }

   

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

   

Hacking with Swift is sponsored by Stream

SPONSORED Build Chat messaging quickly with Stream Chat. The Stream iOS Chat SDK is highly flexible, customizable, and crazy optimized for performance. Take advantage of this top-notch developer experience, get started for free today!

Click here

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.