no data from userdefaults

i have a save function:

func save(){
    if let encoded = try? JSONEncoder().encode(routes) {
          UserDefaults.standard.set(encoded, forKey: saveKey)
    }
}

and an init function:

init() {
        if let data = UserDefaults.standard.data(forKey: saveKey){
            print(data.base64EncodedString)

            if let decoded = try? JSONDecoder().decode([RouteObject].self, from: data) {
                routes = decoded
                return
            }
        }
        routes = []
    }

onload this initiation there is no data. but 3382 bytes how get I the content form data? i don't know where the error is? debug is not possible I did get the info

Answered by Climber1987Muc in 784702022

Hello @Claude31,

I forget to implement a decoder but I missing any error :( thank you for your help :)

greeting

Debug is possible.

For instance, check if try succeeds:

            if let decoded = try? JSONDecoder().decode([RouteObject].self, from: data) {
                print("decode success")
                routes = decoded
                return
            }

Try:

if let data = UserDefaults.standard.data(forKey: saveKey) as? Data {  // <<-- add as? data

Could you also show how RouteObject is defined ?

Hello,

This is a simple environmental object. This Object has only two variables. a getter and init method

I get only a warning: Conditional downcast from 'Data?' to 'Data' does nothing

import Foundation
class RouteList: ObservableObject {
    @Published var routes: [RouteObject]
    let saveKey = "SavedData"
    init() {
        
        if let data = UserDefaults.standard.data(forKey: saveKey) as? Data {
            print(data)

            if let decoded = try? JSONDecoder().decode([RouteObject].self, from: data) {
                routes = decoded
                return
            }
        }
        routes = []
    }
    

    func save() {
            if let encoded = try? JSONEncoder().encode(routes) {
                UserDefaults.standard.set(encoded, forKey: saveKey)
              
            }
        }
    
}

protocol ProtocolName: ObservableObject {
    var routes: [RouteObject] { get }
}
Accepted Answer

Hello @Claude31,

I forget to implement a decoder but I missing any error :( thank you for your help :)

greeting

Just one last thing…

UserDefaults is intended to be used to store settings and so on. These should be small, like a string or a Boolean. Using it to store roughly 3 KiB of data is… well… it’s not wrong, but it’s definitely pushing the boundaries of the expected uses cases for this API.

Additionally, it’s not uncommon for such settings to be lost [1]. If losing this data would cause the user significant grief, I suggest that you store it somewhere else.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] UserDefaults doesn’t lose data without reason. However, this is a complex subsystem and it’s hard to account for all the reasons that might cause data to get lost. For example, on the Mac a user might decide to reset an app’s preference using the defaults tool, not realising that the app is using it to store critical data.

Thank you for information :)

no data from userdefaults
 
 
Q