关于“NSKeyedUnarchiver unarchiveObjectWithFile :”中的 NSException
NSArray *t_annos;
@try
{
NSLog(@" --- pointer before = %ld --- ", (long) t_annos);
t_annos
= [NSKeyedUnarchiver unarchiveObjectWithFile : d_path];
NSLog(@" --- pointer after = %ld --- ", (long) t_annos);
}
@catch (NSException *e)
{
NSLog(@" --- e caught ---");
t_annos = nil;
}
请考虑上面的陈述,情况是:
1
根据文档,如果 d_path 没有指向有效的存档,则应引发异常。但即使 d_path 故意设置为无效路径,也不会捕获异常。
2
在 xcode 模拟器和测试设备(iphone)上测试了代码。尽管模拟器和手机设备都没有捕获任何异常,但手机会按预期取消归档数组,而在模拟器上,程序会停止并在调试器控制台输出:“程序收到信号:“EXC_BAD_ACCESS””。
3
“错误访问”错误应该出现在“unarchiveObjectWithFile”语句中,因为程序在第一个 NSLog 输出后停止。
4
当尝试对单个 NSString 对象进行归档和取消归档时,模拟器和测试设备都没有问题。但即使路径错误,仍然没有捕获到异常。
可能有一些不足之处,希望有识之士帮忙解答。
NSArray *t_annos;
@try
{
NSLog(@" --- pointer before = %ld --- ", (long) t_annos);
t_annos
= [NSKeyedUnarchiver unarchiveObjectWithFile : d_path];
NSLog(@" --- pointer after = %ld --- ", (long) t_annos);
}
@catch (NSException *e)
{
NSLog(@" --- e caught ---");
t_annos = nil;
}
Please consider the above statements, the situation is :
1
According to documentation, an exception should be raised if d_path does not point to a valid archive. But no exception is caught even if d_path is deliberately set with an invalid path.
2
Have tested the code on both the xcode simulator and a test devise (iphone). Although both the simulator and the phone devise do not catch any exceptions, the phone unarchives the array as expected while on the simulator the program stops with an output : "Program received signal : "EXC_BAD_ACCESS"" at the Debugger Console.
3
The "bad access" error should have come at the "unarchiveObjectWithFile" statement since the program stops after the first NSLog output.
4
When try with a single NSString object archived and unarchive, both the simulator and the test device have no problem. But there is still no exception caught even when the path is wrong.
There may be something missing on my part, hope that somebody knowledgable can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,仅当路径中存在文件且不是 NSKeyedArchiver 创建的存档时才会引发异常。
因此,对于 -
1:很可能您设置的无效路径没有指向任何文件,因此返回值为零,没有任何异常。
2:您是否确保模拟器上的路径指向之前由
NSKeyedArchiver
存档的有效文件?最有可能的是,它指向其他一些文件。4:与#1 相同。
According to the documentation, the exception is thrown only when there exists a file at the path and is not an archive created by
NSKeyedArchiver
.So, for -
1: Most likely the invalid path that you set is not pointing to any file, and hence the return value is nil without any exception.
2: Have you ensured the path on the simulator points to a valid file archived by
NSKeyedArchiver
earlier? Most likely, it points to some other file.4: Same thing as #1.