NSCalendar 内部使用的 Objective-C 单例类别
我有一个由两部分组成的问题。首先,如何在 Obj-C 中创建 Singleton 类别?它仅供内部使用,因此不必是万无一失的单例。其次,我可以在 NSCalendar 上创建此类别并使单例成为 autoupdatingCurrentCalendar 吗?考虑到用户在使用应用程序时可能会更改时区,这是否安全?我想避免每次需要时创建 NSCalendar 实例(因为它用于 tableviewcells),但我不想出现时区问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“单例类别”没有任何意义......单例是一个只能实例化一次的类。类别提供了一种扩展类的方法,但不提供对类实例化次数的任何特定控制。
听起来你真的只是想要一个 NSCalendar 的共享实例。如果是这种情况,那么您当然可以声明一个全局变量并在类别中创建一些类方法来访问该全局变量。
"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.
autoupdatingCurrentCalendar
已经是一个单例,所以我不认为为此创建一个类别的意义。至于创建单例类别——当然,这是可能的,与常规单例几乎相同。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.