Kwnstantinos Natsios Kwnstantinos Natsios - 3 months ago 19
iOS Question

How to create a proper singleton for Dictionary in Swift 2

I have the following code

class Test: UIViewController {
var imagesOfChef = [Int : chefImages]()
struct chefImages {
var objidChef: String!
var imageChef: UIImage!
}

}


I fill up this dictionary as soon as the user opens the application.
But i want it to be available also in other Views (swift files)

Lets say in this class

class Test2: UIViewController{

}


How can i create a singleton for this Dictionary so it can be available to other views?

Thanks for your time!

Answer

You can use a static property:

static var imagesOfChef = [Int : chefImages]()

and then you use:

Test.imagesOfChef 

But I suggest avoiding the static approaches as much as possible, you use use the prepare segue or assign the property from outside if possible if Test has Test2.