[英]NSFileManager createFileAtPath works in simulator but fails on device
[英]NSFileManager -createFileAtPath fails with NSInvalidArgumentException
也许我刚刚错过了文档中的内容,但我找不到任何说明这种行为应该在iOS 8中发生变化的内容。
我的应用程序将当前工作目录设置为Documents
目录,然后尝试使用NSFileManager -createFileAtPath
在那里创建一个文件。 在iOS 8之前,这可以正常工作。 在运行iOS 8的设备上,我在调用-createFileAtPath:
得到以下-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
上的path参数是"temp.dat"
- 如果我将其更改为"./temp.dat"
,则调用成功。
我做了一些愚蠢的事情,这只是“偶然”在之前的iOS版本中工作了吗? 或者他们是否有意改变了这里的行为? 或者这是iOS 8中的错误? 采用path
参数的其他NSFileManager方法似乎没有文件名(例如-removeItemAtPath:@"temp.dat" error:&err
成功)。
编辑添加:
这仅发生在物理设备上。 在模拟器中,上面对createFileAtPath
调用成功。
我向Apple公开了一个错误,他们将其作为副本关闭。 虽然这并不一定证实他们认为这是一个错误,但它至少证实了我不是第一个遇到这个错误的人,并且这种行为确实在iOS 8中发生了变化。
目前,解决方案是将./
到文件名,或提供绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.