Cassandra
Cassandra

Reputation: 176

How to store form data in a swift app

I'm working on my first app and I need some help understanding what kind of data storage I need. I'm familiar with PHP, SQL, and databases but I don't know where to start with this project.

The app is pretty much a basic form with text fields, pickers, and uploaded images. At the end, the user will press submit and all of their data will be sent in an email.

What is the best way to store their data, so the user can go to previous screens and have their previously entered info still there. And then what is the best way to store the data after they press submit to send it in an email?,

Thanks so much for your help!

Upvotes: 0

Views: 1950

Answers (2)

Travis M.
Travis M.

Reputation: 11257

If it's just form data that you're storing once for submission, for simplicity sake, I recommend just stuffing it in a global dictionary that you can access from different views. Swift makes this easy by just adding an empty swift file and defining your dictionary at the top:

var myFormData: [String: AnyObject]() 

You can now access "myFormData" form anywhere in your app, add and remove stuff from it.

You shouldn't technically need to "reload previous views" because of the way the navigation stack works. Anything you go back to should hold it's info.

If you really need to save the data to allow the user to close the app and then pick up where they left off much later, then I recommend simply kicking your dictionary to NSUserDefaults. It doesn't sound like something that needs to involve a database.

Upvotes: 2

Ravin Sardal
Ravin Sardal

Reputation: 727

You can use a class called NSUserDefaults

var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() //make an instance
defaults.setObject("Bob", forKey: "myName") //storing a value 
defaults.synchronize() //synchronize data 
println(defaults.objectForKey("myName")!) //retrieve the data 

Upvotes: 1

Related Questions