Properly Store Predetermined Data

Hello,

I'm working on a reference / informational style app and I'm not sure what the correct practice is for storing a lot of data thats predetermined and unlikely (but possible) to change.

A good example of something similar to what I'm working on is a wikipedia page.

Let's say I built a struct for managing this data, and all of the information above is can be broken down and placed into the struct for use.

Currently, I have all of this information in different files and as global constants.

Code Block Swift
Water.swift
let water = MyStruct(...)

Code Block Swift
Ice.swift
let ice = MyStruct(...)

Code Block Swift
Steam.swift
let steam = MyStruct(...)


Currently it's working fine. However I'm planning on having over 500 different files / constants filled with data once the app is fully built.

My questions are:
  • Is having a large amount of global constants bad? (even though they are lazy)

  • Whats the best practice for storing this data?


Note:
I did come across this SwiftUI tutorial by Apple and it's also a good example of what I'm trying to accomplish. What they did inside of it is wrapped up the data in JSON, and then had a codable struct to match it to. Would that be a better approach?
I'm working on a similar set up in my app, I modeled my data off of the Landmarks Tutorial Apple has. My data is stored in a series of arrays in a json file and a swift.file of a class with enums of those data types from the array. I followed the Landmarks tutorial structure to a tea.
Accepted Answer

Is having a large amount of global constants bad? (even though they are lazy)

It depends on what you mean by a “a large amount”. Assuming each item is relatively small, I would not consider 500 items to be a large amount. The main downside with using Swift for this is that a full rebuild of your app will require recompiling 500 extra files.

Whats the best practice for storing this data?

It very much depends on the specifics of your data. There are numerous options, including:
  • Using code, as you outlined above

  • Using a JSON or property list embedded as a resource within your app

  • Using a Core Data database embedded in your app

  • Using on-demand resources

Each of these how its own pros and cons, depending on the size and format of your data and the overall workflow within your app.

You can also implement hybrid approaches. For example, my main issue with using a JSON resource is that it’s weakly typed. With reference to the Landmarks tutorial that connor-rose mentioned, the coordinates property of the JSON has two fields, longitude and latitude, that must be numbers. It’s very easy to make a mistake here. For example, let’s say you copy’n’paste the coordinates from a source that uses a typographically-correct minus sign (U+2796 MINUS SYMBOL), rather than the ASCII one expected in JSON (U+002D HYPHEN-MINUS). It’ll take a you while to track that down in the JSON but, if you used the Swift compiler as you’re suggesting, you’ll find it very quickly.

You can get the best of both worlds by defining your data structure in a strongly-typed language, like Swift, and then exporting that to your ‘transport’ format.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Hey Quinn,

Thanks for the reply! - That's interesting. All leave things as they are for now, using the code as I outlined, but I'll look into setting them up in Core Data as it's the next thing on my to do list anyways.

Thanks!
Properly Store Predetermined Data
 
 
Q