Only today I started to understand UserDefaults in Swift and the question arose, I was able to save data such as label text, variables, but I can't save the setTitle buttons. Maybe someone knows the initializer for this case? more precisely, even save it as it saves, but when I display the code when loading the application, I get an error. I’m trying to do this in this way only instead of the label, respectively, setTitle

'if let labelDef = userDefaults.object(forKey: "labelDef") { labelDisplayScene.text = labelDef as? String }' 

    1 answer 1

    This is how we save the string:

     if let buttonTitle = button.titleLabel?.text { UserDefaults.standard.set(buttonTitle, forKey: "foo") } 

    So we get:

     if let str = UserDefaults.standard.object(forKey: "foo") as? String { button.setTitle(str, for: .normal) } 
    • works) thanks)) it turns out we first take the button text into a variable, and then it works with this variable, which line only contains?) - Vladislav Bublik
    • @VladislavBublik you first extract an option and then work with a non-option variable - Vitaly