Best practice of storing immutable data in the app

I want to make an app that helps prepare for a driving test. Question lists don’t change for years and I want to know the best way to store them in the app. The question may include an image, the question itself, and multiple response options. I guess that I need to do something similar to RW example projects: save photos in assets, and text of questions and answers in plist file. But I have never created plists, are there any articles on this topic? Or maybe there is a better way?

As a first approach I’d store images in some asset catalog (an .xcassets file) and use a JSON file to store questions, answers and image names.

I choose JSON because it is more mainstream but Property Lists can also be used. Swift offers you JSONDecoder and PropertyListDecoder types to parse these files into your own models and use Codable to handle this easily.

A simple JSON could look like this:

{
    “items”: [
        {
            “question”: “some question”,
            “image”: “imageName”,
            “answers”: [
               {
                   “value”: “some answer”
                   “isCorrect”: true
               },
               {
                   “value”: “some answer 2”
                   “isCorrect”: false
               },
               {
                   “value”: “some answer 3”
                   “isCorrect”: false
               }
            ]
        },
       {
            “question”: “some question 2”,
            “image”: “imageName2”,
            “answers”: [
               {
                   “value”: “some answer”
                   “isCorrect”: false
               },
               {
                   “value”: “some answer 2”
                   “isCorrect”: false
               },
               {
                   “value”: “some answer 3”
                   “isCorrect”: true
               }
            ]
        }
    ]
}

Ultimately you can start with any of those and change it later as your app evolves.

1 Like