0

This is my second week learning swift.

Currently, I am trying to create a quote app that generates random quotes. I am storing the quotes in an array. So far the app works well, however, I don't think it's efficient to store the quotes in the view controller file, especially if I am planning on listing 100+ quotes. I came across a comment somewhere suggesting storing a large array of strings in a database or a plist file. I just want to know if someone could suggest the best way to store quotes in a database or something. Any help would be appreciated.

5
  • 1
    Yes large data should not keep in a local array for everytime. If its a local, you can have a json file, plist or use coredata. Commented Feb 8, 2018 at 8:28
  • whats the structure of your Qoutes ? Commented Feb 8, 2018 at 8:28
  • If it just an array of strings -without any additional complexity-, then using UserDefaults might be a good idea, as mentioned in this answer. Commented Feb 8, 2018 at 8:30
  • Also, checking this answer might be useful in your case... Commented Feb 8, 2018 at 8:35
  • UserDefaults is a really bad idea for storing large amounts of data, particularly large amounts of static data. Your best bet for truly large amounts of data would be to put them into an SQLite database and randomly query from that. That would eliminate the memory space issues. For 100 quotes it's probably ok to put them in a JSON file or even text file and load them from there. Commented Feb 9, 2018 at 0:02

3 Answers 3

2

You can save the quotes in JSON file or plist file or xml file and read them from the file from view-controller:

JSON: http://onebigfunction.com/ios/2015/07/01/reading-local-files-ios/

XML: https://www.raywenderlich.com/725/xml-tutorial-for-ios-how-to-read-and-write-xml-documents-with-gdataxml

Plist: https://newfivefour.com/swift-ios-read-value-plist.html

Another option you can use database such as: SQLite, Realm, or userDefaults:

SQLite: https://www.appcoda.com/sqlite-database-ios-app-tutorial/

Realm: https://code.tutsplus.com/tutorials/getting-started-with-realm-database-for-ios--cms-29018

UserDefaults: http://www.ios-blog.co.uk/tutorials/swift/using-nsuserdefaults-with-swift/

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to store quotes and nothing more, than database or UserDefaults will not be a good choice.

It will be better if you save quotes in plist file or JSON file.

3 Comments

Thank you for your answer. What if I just wanted to store two arrays. The first is for the quotes, and the second array is to store the author of the quotes. Is storing both arrays in a plist file or JSON file ideal?
@codescholar I recommend that you store quotes and authors in the same JSON file: [ { "quote": "To be or not to be", "author": "William Shakespeare" }, { "quote": "To be or not to be", "author": "William Shakespeare" } ]
Isn't UserDefaults a plist file? Is a custom plist better than UserDefaults?
0

Instead of doing it in a plist I would really reccommed using Realm Database.

It's a lot easier to setup than CoreData and provides the same functionality, loads of examples and documentation as well.

Comments

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.