关于“NSKeyedUnarchiver unarchiveObjectWithFile :”中的 NSException

发布于 2024-12-06 09:10:51 字数 764 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-12-13 09:10:51

根据文档,仅当路径中存在文件且不是 NSKeyedArchiver 创建的存档时才会引发异常。

用文件取消归档对象:

解码并返回之前编码的对象图
NSKeyedArchiver 写入给定路径的文件。
+ (id)unarchiveObjectWithFile:(NSString *)路径

参数:路径

包含先前编码的对象图的文件的路径
NSKeyedArchiver。

返回值

先前由 NSKeyedArchiver 编码的对象图写入
文件路径。如果路径中没有文件,则返回 nil。

讨论

如果路径中的文件存在,此方法会引发 NSInvalidArgumentException
不包含有效的存档。

因此,对于 -

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.

unarchiveObjectWithFile:

Decodes and returns the object graph previously encoded by
NSKeyedArchiver written to the file at a given path.
+ (id)unarchiveObjectWithFile:(NSString *)path

Parameters: path

A path to a file that contains an object graph previously encoded by
NSKeyedArchiver.

Return Value

The object graph previously encoded by NSKeyedArchiver written to the
file path. Returns nil if there is no file at path.

Discussion

This method raises an NSInvalidArgumentException if the file at path
does not contain a valid archive.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文