-2

I am new to swift, a need to define a global variable class to be used in every controller class.

Thanks.

  • You want just your controller classes to use your 'global variable class' or you want all controller classes, including UIViewController, etc, to use your 'global variable class'? – GoZoner Apr 26 '15 at 16:57
  • can you give me an example. – Robert Brent Apr 26 '15 at 16:58
  • all controller classes – Robert Brent Apr 26 '15 at 16:59
  • 1
    I created a development blog post about this very subject, and then wrote an SO question/answer discussing it. See the SO article at this link: [**How do you share data between view controllers and other objects in Swift?**](http://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift) – Duncan C Apr 26 '15 at 17:09

1 Answers1

0

You can us something like this:

class Favorites {
var favoritesArray:Array <AnyObject>

init(favoritesArray:Array <AnyObject>) {
    self.favoritesArray = favoritesArray
    }
}

and declare it as :

var favoritesInstance = Favorites(favoritesArray:["Harpers_Bazaar_Ch_03_15_0000.jpg"])

and call it from anywhere as :

let x = favoritesInstance.favoritesArray
Koray Birand
  • 1,956
  • 17
  • 22