0

In my application I created a structure with two properties:

struct questionCollection {
var question: String!
var answer: String! 
}

I then created an instance of that:

questionCollections = [questionCollection(question: "What is 1+1?", answer: "2")]

Along with this I have a function that allows the user to add a question:

@IBAction func appendQuestion(sender: AnyObject) {
    questionCollections += [questionCollection(question: questionInput.text, answer: answerInput.text)]
}

My question is what is the easiest way to save the user submitted data? I have only used NSUserDefaults before and I don't know what to do.

I really couldn't find any solution to my problem so if I could get some help that would be great. Thanks

To clarify: All I want to do is have the data stored so can be shown on a label and check to see if the correct answer is chosen. The data does not need to be seen by the user until it comes up in the label. I am also just creating a simple app so I only need this data to be saved on the device.

With what I am doing now I am not sure if I need to save my data but if someone can still answer this if they want for future reference.

Pickle2113
  • 41
  • 6
  • 3
    This is a very broad question. You could save things to a file (text, JSON, XML, property list...), to a database (via Core Data or directly using SQLite), to the cloud (via CloudKit or your own server), to NSUserDefaults, and probably many more options... Without a lot more information about your data (how much there is, how often it is updated, whether you need to sync it between devices, whether you need to search/filter/sort, etc.), it's really difficult to select something for you. – jcaron Feb 24 '16 at 23:34
  • is my question less broad now? – Pickle2113 Feb 25 '16 at 21:39

2 Answers2

1

Have you ever tried using Core Data before? It is Apple's default solution for persistent storage (saving things locally and having them appear again on every load of the app) . More information can be found here. https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/CoreData/index.html

Amloelxer
  • 692
  • 1
  • 7
  • 16
  • I am building an app in a tabbed view template so when you open the app I can't check "Use Core Data". Do you know how I can still use Core Data? – Pickle2113 Feb 25 '16 at 02:44
  • @Pickle2113 the answer can be found here. Best of luck! Hope this was helpful. http://stackoverflow.com/questions/2032818/adding-core-data-to-existing-iphone-project – Amloelxer Feb 29 '16 at 18:02
1

Currently, your structure can be represented as a dictionary value:

 let dictionary:[String:String] = ["What is 1+1":"2", "What is 2+2": "4"]

and you can store a dictionary inside NSUserDefaults:

NSUserDefaults.standardUserDefaults().setObject(dictionary, forKey: "aKey")

I would say, this would be optimal solution in you case ...

Whirlwind
  • 13,478
  • 6
  • 48
  • 124
  • Is there a way I can access the index of a dictionary? Possibly by putting it in an array? – Pickle2113 Feb 25 '16 at 02:47
  • @Pickle2113 Dictionaries are unordered collections...Each value is associated with a unique key. Means, you access the values by the keys. So there is no indexes. But yeah, you could make an array of dictionaries and store them in user defaults. Still, what you should choose, like jcaron already pointed, really depends on your data and how you plan to use it... – Whirlwind Feb 25 '16 at 03:17
  • Is my question less broad now? – Pickle2113 Feb 25 '16 at 21:40