1

In total I have 2 viewcontrolers. The App is the same like this guy made in a tutorial. https://www.youtube.com/watch?v=LrCqXmHenJY&t=40s

To make a start, I made a var:

import UIKit

var list = ["Task 1", "Task 2", "Task 3", "Task 4"]

class FirstViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{

My current problem is, that when I close the app no ToDo's which I created were saved. What do I need to do to save my var list so that my ToDo-List isn't empty when I open it for the next time?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Andreas
  • 19
  • 1
  • 2
  • There's no reason to have a variable outside of the class scope like that. Embed the variable in the relevant class. – Eric Aya Dec 19 '16 at 16:36
  • Look into Core Data if you want to implement a proper persistent storage (or e.g. SQLite, Realm are alternatives), or just use `NSUserDefaults` if you want to start with something easy. – xoudini Dec 19 '16 at 16:36
  • 2
    @xoudini Do not recommend UserDefaults to save data. That is not what it is for. – rmaddy Dec 19 '16 at 17:04
  • 1
    FYI - when reviewing the duplicate question and its answers, ignore answers that tell you to use UserDefaults. That's fine for simple flags and individual preference values, etc. But it is not meant to store users data. Review the answers that suggest proper alternatives to UserDefaults. – rmaddy Dec 19 '16 at 17:12
  • @rmaddy Agreed, which is why my first suggestion was actual databases. Note that I suggested defaults for _"starting with something easy"_ – only because it seems like OP is just getting started with iOS development. Jumping straight into database migrations and such may be slightly discouraging for a beginner. – xoudini Dec 19 '16 at 17:21

3 Answers3

4

You can use UserDefaults to save/load application state or configurations. For complex requirements use CoreData

Writing/Saving before app termination

let defaults = UserDefaults.standard
defaults.set(true, forKey: "Enabled")

Reading/Loading on app launch

let defaults = UserDefaults.standard
let enabled = defaults.bool(forKey: "Enabled")

Read the related Apple docs here.

Zee
  • 1,865
  • 21
  • 42
  • 1
    Zee's answer is the most complete. He even gave you sample Swift 3 code. I'd suggest accepting his answer. (Voted) – Duncan C Dec 19 '16 at 16:53
  • @DuncanC No. This answer is not appropriate. Do not use `UserDefaults` to store data. That is not its purpose. Second, do not needlessly call `synchronize` on `UserDefaults`. – rmaddy Dec 19 '16 at 17:06
  • 2
    The only place you should call `synchronize` is in the `applicationDidEnterBackground` `UIApplicationDelegate` method. – rmaddy Dec 19 '16 at 17:13
2

If you want an easy way to store a list of strings, you can use UserDefaults. Another way to do this is to use Core Data which is more difficult to use. It is usually used to store more complex data structures than an array of strings.

To save the string array to UserDefaults, do this:

UserDefaults.standard.set(list, "myStringArray")

You can replace "myStringArray" with any key you want.

To retrieve it, do this:

list = UserDefaults.standard.stringArray(forKey: "myStringArray")

I suggest you save the array in this method in App Delegate:

func applicationDidEnterBackground(_ application: UIApplication) {

}

For more info on User Defaults, see https://developer.apple.com/reference/foundation/userdefaults

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I disagree with your "...to properly store it, you should use Core Data..." comment. UserDefaults is fine for persisting simple data and would meet the OPs needs perfectly. Core Data is overkill for a simple array of strings, and it has a very steep learning curve. Core Data Is **NOT** a framework for beginners. – Duncan C Dec 19 '16 at 16:52
  • @DuncanC If you say so, I'll remove it. – Sweeper Dec 19 '16 at 16:55
  • 1
    Why is everyone incorrectly suggesting the use of UserDefaults to store user data. That is not its purpose. Write the data to a file (plist) or use a database. Do not use UserDefaults for data. – rmaddy Dec 19 '16 at 17:08
-1

You have very simple needs. Look at NSUserDefaults (now called UserDefaults in Swift 3.)

Save your data to UserDefaults when it changes, and read it back when you display your view controller.

As Eric says in his comments, make list an instance variable of your view controller.

EDIT:

In the comments below, rmaddy pointed out that the app will be saving a to-do list. That will likely grow in complexity from an array of a handful of items to a larger array or potentially a more complex structure.

Given that, you might want to look at saving your data to a plist file in the documents directory (quite simple) or using NSCoding if you have custom objects other than the small list of "property list" types that can be saved to a plist.

Both of those solutions read and write the data all at once to a file. That might be ok for a to-do list app.

If you need to change parts of a larger data-set and leave the rest unchanged then it's time to consider a database like Core Data.

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • No, do not use UserDefaults to store data. There are proper solutions to store data. – rmaddy Dec 19 '16 at 17:07
  • 1
    @rmaddy, I respectfully disagree with you. For storing small amounts of simple data `UserDefaults` is a very good fit. Agreed about only calling synchronize from the applicationDidEnterBackground notification method, but otherwise, it's a good solution for this simple use-case. What would you recommend as an alternative for storing simple data like this? – Duncan C Dec 19 '16 at 17:16
  • 2
    The data seems simple in this question because it appears to be only a trivial string array with only a few entries. But a real TODO app will end up with far more data than a simple string array. Suggesting the use of UserDefaults now simply leads the OP down the wrong path as the app data increases in both size and complexity. BTW - not my down vote. – rmaddy Dec 19 '16 at 17:20
  • Ok, point taken. I just glanced at the data shown in the question, and missed the bit about "To Dos". For data of much complexity user defaults is not a good choice. – Duncan C Dec 19 '16 at 17:57