MGA
MGA

Reputation: 3751

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.

Upvotes: 0

Views: 357

Answers (2)

Caleb
Caleb

Reputation: 125007

"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.

Upvotes: 1

omz
omz

Reputation: 53561

autoupdatingCurrentCalendar is already a singleton, so I don't see the point of creating a category for that. As for creating singleton categories – sure, that's possible, pretty much the same as a regular singleton.

Upvotes: 0

Related Questions