iPhone - daylightSavingTimeOffset崩溃(???)

Spa*_*Dog 1 iphone objective-c

我在应用程序即将退出时调用的块中有这两行

NSTimeZone* systemTimeZone = [NSTimeZone systemTimeZone];
NSTimeInterval delta = [systemTimeZone daylightSavingTimeOffset];
Run Code Online (Sandbox Code Playgroud)

我有几周的这些线.他们工作得很好.现在,应用程序在显示此错误的第二行崩溃:

- [NSCFString daylightSavingTimeOffset]:无法识别的选择器发送到实例0x1534b0

因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSCFString daylightSavingTimeOffset]:无法识别的选择器发送到实例0x1534b0'**

什么?任何线索?谢谢.

Joe*_*Joe 5

如果按顺序调用这些行并且您收到该消息,则表明您已经过度释放了systemTimeZone.恰好有效的NSString*是占用存储缓存的systemTimeZone的内存.

根据文件:

特殊注意事项
如果获得系统时区,则应用程序将对其进行高速缓存,如果用户随后更改了系统时区,则不会更改.下次调用systemTimeZone时,您将返回最初获得的时区.您必须调用resetSystemTimeZone来清除缓存的对象.

所以考虑一下

NSTimeZone *systemTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"%lf", [systemTimeZone daylightSavingTimeOffset]); //This works
[systemTimeZone release]; //Testing release do not actually do this

systemTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"%lf", [systemTimeZone daylightSavingTimeOffset]); //This works
[systemTimeZone release]; //Testing release do not actually do this

systemTimeZone = [NSTimeZone systemTimeZone];
NSLog(@"%lf", [systemTimeZone daylightSavingTimeOffset]); //This crashes EXC_BAD_ACCESS
//The cached systemTimeZone at this point has been over released.
Run Code Online (Sandbox Code Playgroud)

在我得到我的地方EXC_BAD_ACCESS,如前所述,NSString*也可能驻留在那里,这将导致异常被引发.所以只要确保你没有在任何地方不正确地释放它.