Search code examples
iosjsonswiftxcodeuserdefaults

Saving the data of a table view in Swift 3


I'm trying to build a simple to-do list app with Swift 3 and Xcode 8. It includes a text input box, a submit button, and a table, where each cell includes a title and a subtitle that shows the date. So far, all of this has worked.

Currently, I'm trying to get it to save when I leave the app. I've tried user defaults (which I have succesfully used in the past, but wasn't working for this), core data (which seemed like a lot of excess code, will do if I have to), and a JSON file I tried to read and write from with SwiftyJSON (have also succesfully used this). None of these have worked.

What would be the simplest/best way to solve my problem?

Thank you!


Solution

  • The simplest way to persist data locally in those circumstances is probably to use NSCoding. See the "Persist Data" section in Apple's Start Developing iOS Apps (Swift) guide.

    You might also want to take a look at Apple's sample code Lister (for watchOS, iOS, and OS X), which demonstrates a productivity app for iOS and OS X that enables you to create and share to-do lists across devices. A user can store their documents both locally or in iCloud, but this may be more than you need for your use case.