Roman
Roman

Reputation: 154

Save dictionary to UserDefaults

I'm trying to store a dictionary in UserDefaults and always get app crash when the code runs. Here is the sample code which crashes the app when it is executed. I tried to cast it as NSDictionary or make it NSDictionary initially - got the same result.

class CourseVC: UIViewController {

let test = [1:"me"]

override func viewDidLoad() {
    super.viewDidLoad()

    defaults.set(test, forKey: "dict1")

    }

}

Upvotes: 10

Views: 13476

Answers (2)

Omar Albeik
Omar Albeik

Reputation: 1344

Dictionaries are Codable objects by default, you can use the following extensions to save them to UserDefaults

extension UserDefaults {
    func object<T: Codable>(_ type: T.Type, with key: String, usingDecoder decoder: JSONDecoder = JSONDecoder()) -> T? {
        guard let data = self.value(forKey: key) as? Data else { return nil }
        return try? decoder.decode(type.self, from: data)
    }

    func set<T: Codable>(object: T, forKey key: String, usingEncoder encoder: JSONEncoder = JSONEncoder()) {
        let data = try? encoder.encode(object)
        self.set(data, forKey: key)
    }
}

They can be used like this:

let test = [1:"me"]
UserDefaults.standard.set(object: test, forKey: "test")

let testFromDefaults = UserDefaults.standard.object([Int: String].self, with: "test")

This extension and many others are part of SwifterSwift, you might want to use it for your next iOS project :)

Upvotes: 33

Lenin
Lenin

Reputation: 685

To store a NSDictionary (with non-string key) in NSUserDefaults you need to convert them to NSData first. Try this

let test = [1:"me"]
override func viewDidLoad() {
    super.viewDidLoad()
    let data = NSKeyedArchiver.archivedData(withRootObject: test)
    let defaults = UserDefaults.standard
    defaults.set(data, forKey: "dict1")
    if let data2 = defaults.object(forKey: "dict1") as? NSData {
        let dict = NSKeyedUnarchiver.unarchiveObject(with: data2 as Data)
        print(dict)
    }
}

Upvotes: 1

Related Questions