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.

  • 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. – TheTiger Feb 08 '18 at 08:28
  • whats the structure of your Qoutes ? – Muhammad Shauket Feb 08 '18 at 08:28
  • If it just an array of strings -without any additional complexity-, then using UserDefaults might be a good idea, as mentioned in [this](https://stackoverflow.com/a/37357905/5501940) answer. – Ahmad F Feb 08 '18 at 08:30
  • Also, checking [this answer](https://stackoverflow.com/a/40278645/5501940) might be useful in your case... – Ahmad F Feb 08 '18 at 08: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. – David Berry Feb 09 '18 at 00:02

3 Answers3

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.

Ilya Kharabet
  • 4,203
  • 3
  • 15
  • 29
  • 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 Feb 08 '18 at 08:49
  • @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" } ] – rodskagg Feb 08 '18 at 09:18
  • Isn't UserDefaults a plist file? Is a custom plist better than UserDefaults? – Marcy Aug 27 '19 at 18:03
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.

kd02
  • 422
  • 5
  • 14