简体   繁体   中英

Alternative to a singleton implementation in Swift?

I'm working on an app which has a ContentManager class. This class migrates files from the bundle, and performs other checks when the app is first opened. It's next used in a ViewController at the end of a rather long chain of ViewControllers. As far as I can tell, there are two methods of implementing this:

  1. Instantiate the class in the first ViewController, and pass it through the segues to the final ViewController.

  2. Create a singleton sharedInstance in the first ViewController and access it where needed using that sharedInstance.

It seems a lot cleaner and simpler to create the singleton. However, I've read a lot about how singletons are bad. I'm wondering if I'm missing some alternative way of doing this without using a singleton or messily passing the instance through all the view controllers?

Many thanks

Here is the blog that may help on what you are looking for

Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM