0

I have several sets of data that contains the same declared variables but different values, each set in a different file for clarity.

Here is a simplified version of what I'm trying to achieve :

In dataSet1.swift :

let someData = ['Multidimensional Array from Set 1']
let otherData = ['An other Multidimensional Array from Set 1']

In dataSet2.swift :

let someData = ['Multidimensional Array from Set 2']
let otherData = ['An other Multidimensional Array from Set 2']

The app would load one or the other dataset according to the user preference. Eg. in ViewController.swift, I would like to use the data from the loaded dataSet, for example,

print(someData[0]) would show the string Multidimensional Array from Set X according to the current loaded dataSet.

Is that possible, or is there a better practice ? I thought of using classes, but I don't want to store all the dataSets in the memory, only the one the user wants.

2
  • 1
    Do you already have the data when you're building the app? If yes, use a json file (or one file per dataset) to store it and package those with the app. You can then conditionally load the one that you need as per your user preference. And to make things easier to deal with, if you end up using json files, have a look at github.com/SwiftyJSON/SwiftyJSON The "Creating Model Objects from Values Extracted from JSON" section might be helpful for you here: developer.apple.com/swift/blog/?id=37 Commented Mar 27, 2017 at 21:24
  • Yes, makes sense. Thanks. Commented Mar 27, 2017 at 22:01

1 Answer 1

1

Realistically, if you're loading only data from a separate file, you may be better off using a more standardized format, than direct code; or using code to output the standardized format as part of your build process.

Some very common formats to consider:

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

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.