gpt4 book ai didi

ios - NSFileManager -createFileAtPath 因 NSInvalidArgumentException 而失败

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:51 25 4
gpt4 key购买 nike

也许我只是遗漏了文档中的某些内容,但我找不到任何说明此行为应该在 iOS 8 中更改的内容。

我的应用程序将当前工作目录设置为 Documents 目录,然后尝试使用 NSFileManager -createFileAtPath 在那里创建一个文件。在 iOS 8 之前,这工作正常。在运行 iOS 8 的设备上,我在调用 -createFileAtPath:

时得到以下信息
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSFileManager fileSystemRepresentationWithPath:]: nil or empty path argument'


这是重现问题的最小代码片段:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:docsDir];
[[NSFileManager defaultManager] createFileAtPath:@"temp.dat" contents:nil attributes:nil];

请注意 createFileAtPath 上的路径参数是 "temp.dat" - 如果我将其更改为 "。/temp.dat",调用成功。

我是不是在做一些愚蠢的事情,这只是“不小心”在以前的 iOS 版本中工作?还是他们故意改变这里的行为?或者这是 iOS 8 中的错误?其他采用 path 参数的 NSFileManager 方法似乎只需要一个文件名就可以了(例如 -removeItemAtPath:@"temp.dat"error:&err 成功)。

编辑添加:

这只发生在物理设备上。在模拟器中,上述对 createFileAtPath 的调用成功。

最佳答案

我向 Apple 打开了一个错误,他们将其作为副本关闭。虽然这不一定证实他们认为这是一个错误,但它至少证实了我不是第一个遇到这个问题的人,而且这种行为确实随着 iOS 8 发生了变化。

目前,解决方案是在文件名前添加 ./,或提供绝对路径。

关于ios - NSFileManager -createFileAtPath 因 NSInvalidArgumentException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26001189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com