I've run into a problem. I want to have a few arrays that are accessible (get/set) from all other classes.
I tried working with a Singleton but I don't know how to set the data. How do you set that data and/or are there any alternatives?
I've run into a problem. I want to have a few arrays that are accessible (get/set) from all other classes.
I tried working with a Singleton but I don't know how to set the data. How do you set that data and/or are there any alternatives?
To have data accessible from all your classes you have two possibilities:
Using a Singleton. Hated by some, useful to others. With this pattern you basically have a global access point to some data. Problems: kills multithreading (if writing at the same time to the Singleton) and also you have now been coupled to the Singleton's implementation. Good discussion on how to create a Singleton in Swift here. Also lots of tutorials out there on how to create a Singleton in Swift
injecting your data (dependencies). You can use a Framework for that like Typhoon
Note: also, you can write data to a file, use NSUserDefaults to pass data between classes, Core Data or other horrible design decisions. These two first are the most widely used for a reason