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?
Asked
Active
Viewed 585 times
0
-
1Check out the answers here - http://stackoverflow.com/questions/26233067/simple-persistent-storage-in-swift – lostInTransit Mar 24 '15 at 14:51
1 Answers
4
Use NSUserDefaults.
Save array:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(myArray, forKey: "myarray")
defaults.synchronize()
Read array:
myArray = defaults.dataForKey("myarray") as Array

4thSpace
- 43,672
- 97
- 296
- 475
-
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' – Stewart Hering Mar 24 '15 at 15:13
-
1While 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. – Cyrille Sep 07 '16 at 16:10