Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Multiple Singletons in 1 class

Tags:

ios

swift

I have a situation where I may need to declare two singletons in a class

class ImageManager {

    static var imageCache = NSCache()

    static var imageManager = ImageManager()

    (...)
}

Is there any danger to this ?

like image 326
ray john Avatar asked Sep 07 '26 23:09

ray john


1 Answers

Generally, there is no problem with multiple static variables in a single class. Only one of them should be called singleton - namely, the imageManager, because that's the instance that's going to be unique in your design.

However, if ImageManager need the exclusive use of NSCache, it is better to make imageCache an instance property of ImageManager. Should other static methods need to access the cache, they could always gain access to it through imageManager.imageCache.

like image 163
Sergey Kalinichenko Avatar answered Sep 11 '26 02:09

Sergey Kalinichenko