0

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?

Seyed Parsa Neshaei
  • 3,470
  • 18
  • 30
Stewart Hering
  • 304
  • 4
  • 14

1 Answers1

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
  • 1
    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. – Cyrille Sep 07 '16 at 16:10