Kwnstantinos Natsios Kwnstantinos Natsios - 1 year ago 50
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 Source

You can use a static property:

static var imagesOfChef = [Int : chefImages]()

and then you use:


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.