0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kaaseter
  • 359
  • 1
  • 5
  • 6

1 Answers1

0

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

Community
  • 1
  • 1
Diego Freniche
  • 5,225
  • 3
  • 32
  • 45