1

Is it possible to pass parameters to a Singleton class? If so how?

This is a snippet of one of my Singleton classes:

class NotificationManager: NSObject {

static let sharedManager = NotificationManager()
var tabBarController: UITabBarController!

private override init() {
    super.init()
}

In the case of this Singleton, the public property tabBarController must always be set before being used.

I'm wondering if its possible to pass the correct UITabBarcontroller object on start up, as I assume this is when my Singleton class is being instantiated? This would prevent the class from ever being implemented incorrectly. Not that I ever expect this to be a problem as is.

Also, is it perfectly fine to use Singleton classes in this instance? To manage a specific set of operations? In the past I was always passing objects around, especially managedObjectContext, however now I've found Singleton classes to make everything much cleaner, nicer, and safer (as there is only ever one stance) in large apps. I can understand passing objects between classes is fine for smaller apps, but once they can large it can get pretty messy.

In my most recent app I'm also using a shared Singleton class between my iOS and watchOS app that defines a few global variables, such as server endpoints, and environments. This appears to be the best way to go about defining variables that require this access.

I've had friends tell me that Singletons are bad to use, and from what I've Google'd, it really seems like a mixed bag. In my case, they have made my code way easier to manage, and handle.

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
toast
  • 1,860
  • 2
  • 26
  • 49

1 Answers1

1

You can set up a singleton anyway you like. You will just have to change the way it's initialized/organized. There are some examples in this question. For some general critiques of the pattern you can read here

My own opinion is that it's generally good to avoid singletons, but sometimes they are necessary. However I tend to say singletons should only be used when the can be initialized when the app starts (App Delegate), and can/should be used and alive throughout the entire app's lifetime.

I would warn against using singletons having to do with your user interface. It appears you're capture a UITabBarController. I would strongly recommend against this, and instead use delegation or Notifications to respond to events. This is partially because of general design weirdness, but it also complicates app restoration and the memory footprint of your app.

Community
  • 1
  • 1
GetSwifty
  • 7,568
  • 1
  • 29
  • 46
  • Thats good to know. As for `UITabBarController`, in this class, I'm simply updating the badge icon of a tab. If I were to use `Notifications` for this I would need to have a notification handler in each class where the relevant updating function is called. Which would make it all a bit messy. If I were to use a delegate, that would mean I would need to have the delegate protocol function in each class where I used the in question function. I think the delegate would look nicer in this instance. Thoughts? – toast Feb 10 '17 at 04:11
  • 1
    I would say that's a perfect use for Notifications :) I would recommend having a static method or function you can call so you just have to do NotificationHandler.sendNotificationsUpdateNotification() from anywhere in your app, and have your tab controller listen for that. – GetSwifty Feb 10 '17 at 15:55