Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save and read array of array in NSUserdefaults in swift?

I need create an array to add objects with this format like a dictionary in Swift : ["key1": "value1", "key2": "value2"]

When I try to save it with NSUserDefaults all is correct, but when read NSUserDefaults with the key this crashes. What type of data does my var obj need?

let def = NSUserDefaults.standardUserDefaults() var key = "keySave" var element: AnyObject!  var array1: [AnyObject!] = [] array1.append(["key1": "val1", "key2": "val2"]) array1.append(["key1": "val1", "key2": "val2"])  //save var savestring : [AnyObject!] savestring = array1 var defaults = NSUserDefaults.standardUserDefaults() defaults.setObject(savestring, forKey: key) defaults.synchronize()  //read var obj: [AnyObject!] = [] if(obj != nil){     print("size: ")     print(obj.count) //vary long value confused..     element = obj[0]  //crash     print(element.objectForKey("key1")) } 
like image 365
user3745888 Avatar asked Aug 07 '14 10:08

user3745888


People also ask

Can I store array in UserDefaults Swift?

You can only store arrays of strings, numbers, Date objects, and Data objects in the user's defaults database.

How do you save a Userdefault string in Swift?

To store the string in the user's defaults database, which is nothing more than a property list or plist, we pass the string to the set(_:forKey:) method of the UserDefaults class. We also need to pass a key as the second argument to the set(_:forKey:) method because we are creating a key-value pair.

How are arrays stored in Swift?

In Swift, we have a datatype ContiguousArray that ensures our array is stored in a contiguous area of memory.


1 Answers

The question reads "array of array" but I think most people probably come here just wanting to know how to save an array to UserDefaults. For those people I will add a few common examples.

String array

Save array

let array = ["horse", "cow", "camel", "sheep", "goat"]  let defaults = UserDefaults.standard defaults.set(array, forKey: "SavedStringArray") 

Retrieve array

let defaults = UserDefaults.standard let myarray = defaults.stringArray(forKey: "SavedStringArray") ?? [String]() 

Int array

Save array

let array = [15, 33, 36, 723, 77, 4]  let defaults = UserDefaults.standard defaults.set(array, forKey: "SavedIntArray") 

Retrieve array

let defaults = UserDefaults.standard let array = defaults.array(forKey: "SavedIntArray")  as? [Int] ?? [Int]() 

Bool array

Save array

let array = [true, true, false, true, false]  let defaults = UserDefaults.standard defaults.set(array, forKey: "SavedBoolArray") 

Retrieve array

let defaults = UserDefaults.standard let array = defaults.array(forKey: "SavedBoolArray")  as? [Bool] ?? [Bool]() 

Date array

Save array

let array = [Date(), Date(), Date(), Date()]  let defaults = UserDefaults.standard defaults.set(array, forKey: "SavedDateArray") 

Retrieve array

let defaults = UserDefaults.standard let array = defaults.array(forKey: "SavedDateArray")  as? [Date] ?? [Date]() 

Object array

Custom objects (and consequently arrays of objects) take a little more work to save to UserDefaults. See the following links for how to do it.

  • Save custom objects into NSUserDefaults
  • Docs for saving color to UserDefaults
  • Attempt to set a non-property-list object as an NSUserDefaults

Notes

  • The nil coalescing operator (??) allows you to return the saved array or an empty array without crashing. It means that if the object returns nil, then the value following the ?? operator will be used instead.
  • As you can see, the basic setup was the same for Int, Bool, and Date. I also tested it with Double. As far as I know, anything that you can save in a property list will work like this.
like image 128
Suragch Avatar answered Oct 05 '22 02:10

Suragch