28

I want to make a global array of custom objects that can be accessed throughout the app (AppDelegate, ViewController classes, TableViewController classes, etc). I have researched for a way to do it, but have not found an answer. I have tried making giving the array a public scope, but I get a complier warning which says Declaring public variable from internal class and when I try to access it in a different file, I get an error that says Use of unresolved identifier 'arrayObjectives'

How would I go about making that array globally accessible to all files in the application and where would I instantiate that array?

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Blake Morgan
  • 767
  • 1
  • 7
  • 25

4 Answers4

22

From the Swift Programming Language -

Global variables are variables that are defined outside of any function, method, closure, or type context

So you can simply declare your variable at the top of any file straight after the import statements.

However, I would suggest you seriously reconsider. Generally globals aren't a good idea. You are better off with properties on a singleton or using dependency injection.

Your second question "where would I instantiate the array?" is part of the reason why globals are bad - their lifecycle isn't well defined in terms of your other objects. A singleton that is initialised on first use eliminates this issue.

Community
  • 1
  • 1
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • 1
    If you use `private` as access modifier the global variable will just be global in the swift file you declare it... Which is quite useful in circumstances where you want a static variable (as `class`is not supported yet) – Soko Jan 05 '15 at 12:01
  • 1
    Globals are a completely natural and idiomatic part of Swift. – Fattie Feb 21 '17 at 14:34
  • 2
    Sure, and C and a bunch of other languages. It still doesn't mean they are a good idea. Global constants are OK but global variables have many drawbacks in a multithreaded, object oriented, asynchronous environment. – Paulw11 Feb 21 '17 at 20:11
13

You can set global Array like this way :

import UIKit

var abc : String = String()

and you can access it in any other file like :

abc = "ABC"
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Where would I set that? I know I can't do it in a function, but when I try to do it in a class, I get the complier warning mentioned above. – Blake Morgan Oct 28 '14 at 05:03
  • 1
    Exactly where they showed you - outside of any function or class – Paulw11 Oct 28 '14 at 05:18
  • yes this way we can declare and access the global variables.. :) – Dharmesh Kheni Oct 28 '14 at 05:20
  • hmm, i have defined a var outside the class in my firstVC but i am not able to get the values in another view? i have put var scannedVisitors = [Visitor]() in firstVC and in another in viewDidLoad() { print("Visitors \(scannedVisitors)") but i get just Visitors [folder_name.Visitor, folder_name.Visitor, etc and not the data?? – alex Dec 18 '15 at 14:08
13

Try making a new Swift file with this:

struct Constants {

  static let appName: String = "My App"

  struct Colors {

    static let colorTextStandard = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3) //#000000

  }

  struct Data {

    static var myStrings = [String]()  // Yea, this is not a constant, but that's alright...

  }

}

You can then refer to those global constants (or you can make them variables) using:

Constants.appName

or

Constants.Colors.colorTextStandard

or

Constants.Data.myStrings = [stringOne, stringTwo]
sean
  • 3,484
  • 5
  • 27
  • 45
5

This is how I did it...

class MessageViewCell {
    struct MessageViewCellHeightCache {
        static var cache: [String:CGFloat] = Dictionary<String, CGFloat>()
    }
}

And I accessed it as follows:

MessageViewCell.MessageViewCellHeightCache.cache["first"] = 12.0
anthonyliao
  • 1,314
  • 1
  • 8
  • 8