2

I'm relatively new to iOS development, and yet this is a problem I just keep coming back to. In the last app I made, a high-school textbook definition review app, I had a large amount of text that was never changed. As use of global variables is frowned upon, I looked to alternatives like Singletons (how these would be more elegant eludes me, feels redundant), prepareForSegue (solves the issue, but it feels like there must be a better way) and permanent storage (makes no sense as the data is never altered, but it provides a central repository). Naively, I picked the latter, implementing a plist, diminishing performance.

My interpretation of use of singleton for this problem (in case my problem stems from misunderstanding):

class Global {
    let array = ["item1","item2",...,"item1042"]
}

Now I can access array from other classes by Global.array. My array is in fact a 3-dimensional array, however I don't see how that would change the issue (would have preferred to create a class, with many instances, but that felt like further complicating an issue simply for readability).

Aside from using coreData, which again seems like overkill, I'm at my wits end.

I want to use a single, unchanged array/dictionary throughout all my classes, preferably without copying it into every class.

What is the proper style to achieve this?

I apologise if this has already been answered (it seems to general not to have been), but whatever answers I found were either too complex for my level of understanding, or unhelpful.

Max Fest
  • 45
  • 1
  • 4

3 Answers3

0

How about

class Global {
    static let array = ["item1","item2",...,"item1042"]
}
Siyavash
  • 970
  • 9
  • 23
  • I am aware this works, but is this the 'proper' way to do it? Edit: Can you explain how this is better than a global variable? – Max Fest Sep 10 '17 at 16:43
0

Use a struct to achieve this. Here is an example:

struct Constants {
  private init() {}

  static let array = ["item1","item2",...,"item1042"]
  static let exampleStringConstant = "exampleStringConstant"
  static let deviceId = UIDevice.current.identifierForVendor?.uuidString
  static let isRunningOnSimulator: Bool = {
    #if (arch(i386) || arch(x86_64)) && (os(iOS) || os(watchOS) || os(tvOS))
      return true
    #else
      return false
    #endif
  }()
}
0

There are many ways to persist data in a single app session. But, among all these, I prefer to create a variable in AppDelegate and use it in the app using a shared instance. This way you won't need to create a separate singleton or any other class with static variable or any kind of global variable.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
    let array = ["item1","item2","item1042"]
    //....
}

class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        print((UIApplication.shared.delegate as? AppDelegate)?.array)
    }
}

Also, in case you want to persist data within multiple app sessions, you can use UserDefaults, Core Data or File System.

PGDev
  • 23,751
  • 6
  • 34
  • 88