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?

1

1 Answer 1

4

Use NSUserDefaults.

Save array:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(myArray, forKey: "myarray")
defaults.synchronize()

Read array:

myArray = defaults.dataForKey("myarray") as Array
Sign up to request clarification or add additional context in comments.

2 Comments

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'
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.