在我们的应用程序中,我们使用NSCalendar来处理NSDate对象,例如获取day组件。
这个NSCalendar实例是一个单例对象,因为我们在短时间内进行了大量的日期计算,我们注意到使用[NSCalendar alloc initWith.]创建新实例每次我们需要的时候,都会花费太多的时间。
问题是,如果在整个应用程序执行期间保持一个NSCalendar实例,则此实例将保持时区属性不变,即使时区发生了更改。
因此,问题在于,我们希望检测时区的变化,并通过以下两种方法作出适当的反应:
self.singletonCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]或
self.singletonCalendar.timezone = [NSTimezone localTimezone];我们正在讨论的可能的解决办法:
谢谢尼可拉斯。
通过Putz1103实现了拟议的解决方案:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChangeNotification) name:UIApplicationSignificantTimeChangeNotification object:nil];
- (void)significantTimeChangeNotification
{
NSTimeZone *localTimezone = [NSTimeZone localTimeZone];
[KIDateUtils setCalendarTimezone:localTimezone];
}发布于 2014-01-23 15:39:35
我会做两个选项的混搭。我会更新它在应用程序启动(或进入前台)。但是,当应用程序当前处于前台时,您可以为时间的变化设置一个监听器,比如answer here。
"UIApplicationDelegate" has a method called "applicationSignificantTimeChange:" that gets called when there is a significant change in the time.
发布于 2014-01-23 15:41:49
让您的单例寄存器用于UIApplicationSignificantTimeChangeNotification通知。当收到此信息时,更新单例中的NSCalendar实例。
https://stackoverflow.com/questions/21312651
复制相似问题