Understanding How UserDefault Stores an Array of Strings

Asked 2 years ago, Updated 2 years ago, 93 views

UserDefault saves the array of Strings and tries to reload it.
However, when I read it, it gets nil and the application drops due to an error.

I'm trying with code on Playground, but please tell me how I can save and load it in an array of strings

import UIKit

let str = ["aaa", "iii", "uuu" ]

let defaults = UserDefaults()

defaults.set(str, forKey: "str")

defaults.synchronize()

// This is where the error occurs
defaults.array (forKey: "str") as! [String]

swift ios swift3

2022-09-30 19:43

1 Answers

In conclusion, the method is used correctly, but the "Trying with code in Playground" test is wrong.

UserDefaults (NSUserDefaults up to Swift2) but not running on Playground (with Platform as iOS) has been reported in the Xcode 7 era, but it seems to still exist in the latest version of Xcode (8.2.1).

NSUserDefaults no longer saving values

(Search "UserDefaults Playground" and you'll find many more.)

If you are trying to use UserDefaults (on iOS), try creating a simple Single View project.

import UIKit

classViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from anib.
        letstrArr=["aaa", "iii", "uuuu" ]

        let defaults = UserDefaults()

        defaults.set(strArr, forKey: "strArr")

        defaults.synchronize()

        // No errors on simulators/actual machines
        print(defaults.array(forKey: "strArr") as![String])//->["aaa", "iii", "uuuu" ]
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I changed it to strArr because it's disgusting to put something that's not even a string in the variable named str, but everything else is exactly the same as your code.

In addition, as mentioned in the article above, defaults.synchronize() is not really required for this kind of usage.

In addition, extracting values from UserDefaults can be nil under various conditions, so even if it is a trial code to try the feature, it is not recommended to use an operator of type "Crash My App" like as!.

override func viewDidLoad(){
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from anib.
    let defaults = UserDefaults()
    iflet defArr=defaults.array(forKey: "strArr") as?[String]{
        // When the app runs after the second time (when UserDefaults is already set), we run here.
        print(defArr)//-> ["aaa", "iii", "uuuu" ]
    } else{
        // When the app runs for the first time (when UserDefaults is empty), it runs here
        print("Setting new array to 'defArr'")
        letstrArr=["aaa", "iii", "uuuu" ]

        defaults.set(strArr, forKey: "strArr")
    }
}

"nil You should try to code ""safe"" using conditional bindings (even when writing a little trial code)."


2022-09-30 19:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.