Question regarding storing a list of data along with the app.

Discussion in 'iOS Programming' started by techwoman, Oct 18, 2016.

  1. techwoman macrumors newbie

    Joined:
    Nov 22, 2014
    Location:
    USA
    #1
    I have a list of 175 items that I need to store somewhere for the lifetime of the app. I couldn't find an API that could provide me with this list of items, hence I would need to store it somewhere in the app. I could hardcode these values, but I think 175 is a big number and would be better to store somewhere in a file maybe. I can also use SQLite I guess, but it's a very basic small app and I don't wish to make it heavy using SQLite.

    My app would read this list and store it in an array before displaying on the UITableView. I was going through the data-storage options on Apple's site(https://developer.apple.com/icloud/documentation/data-storage/index.html) and looks like I could use the 2nd option of storing file in Cache directory since it doesn't even need a backup.

    Which other options do you guys think I could use? My app would just read the list and not perform any add/modify/delete actions on it.
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Use a plist, that's what they're for. Write a little bit of code that builds your NSArray of items and writes it out using NSArray writeToFile. Then save the plist file and add it to your project. It will end up in your app bundle on the device. In your app use NSArray arrayWithContentsOfFile to read in the file. Presto, you'll have the array in your app.

    If you prefer you can edit the plist file in Xcode instead of writing it out and adding it.
     
  3. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #3
    You can add almost any file to the app bundle and read it into your app.

    You could also just create you own NSObject with the array already in it and then just pass it onto where ever you need it.

    Keep it simple!
     
  4. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #4
    If this were a static list that would never change, I would probably store it as a plist as PhoneyDeveloper suggested. But sometimes, if this is a cross-platform list that also gets used by other platforms like Android or a Node.js backend, I would probably save it as a JSON file and then use a JSON <-> plist converter tool. You could also simply store it as JSON and use NSJSONSerialization to read it, but that would be a bit much.
     
  5. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #5
    Using a JSON file would be very cross platform, but the author still needs to know where to store the file and how to access it from the app. The direction hinted at was to use some sort of database like SQL or Coredata, both of which would be extreme overkill for what he is seeking.

    I go back to my idea, create an object using NSObject and simple pre-code the data in an array which is accessible from the object. Simple sweet, and easily accessible. I could even write the code in minutes (objective C) then al the author has to do is add that object to the view which has the table, initialize it and use the array.

    Why make the author find the app bundle and then load the file from the app bundle?
     

Share This Page