I am creating an app in Swift that manages tasks based off of priority. I currently place the tasks into an array. Does anybody know how I can save this array so that when I open the app I will still be able to access the data?
1 Answer
Use NSUserDefaults.
Save array:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(myArray, forKey: "myarray")
defaults.synchronize()
Read array:
myArray = defaults.dataForKey("myarray") as Array
2 Comments
Stewart Hering
the save array seems to be working but when i put in the code to read the array i get this error: Cannot convert the expression's type '()' to type 'String'
Cyrille
While this is a good starting point for a newcomer, please be aware that
NSUserDefaults is by no means a replacement for a full-fledged persisting data store. When you're more comfortable, you should move on to real solutions like Core Data.