Search code examples
objective-csingletonnscalendarobjective-c-category

Objective-C Singleton Category for internal use of NSCalendar


I have a two part question. First, how can you create a Singleton category in Obj-C? It would just be for internal use so it does not have to be foolproof singleton. Secondly, can I create this category on NSCalendar and have the singleton be an autoupdatingCurrentCalendar? Is this safe considering a user might change timezones while using the application? I want to avoid creating an instance of NSCalendar each time I need one (since it is used for a tableviewcells) but I don't want to have timezone problems.


Solution

  • "singleton category" doesn't make any sense... A singleton is a class that can be instantiated no more than once. Categories provide a way to extend classes, but don't give you any particular control over how many times the class is instantiated.

    It sounds like you really just want a shared instance of NSCalendar. If that's the case, then you can certainly declare a global variable and create some class methods in a category that give you access to that global variable.