[英]NSFileManager -createFileAtPath fails with NSInvalidArgumentException
Maybe I've just missed something in the documentation, but I can't find anything that says this behavior should have changed in iOS 8. 也许我刚刚错过了文档中的内容,但我找不到任何说明这种行为应该在iOS 8中发生变化的内容。
My app sets the current working directory to the Documents
directory, then tries to create a file there using NSFileManager -createFileAtPath
. 我的应用程序将当前工作目录设置为
Documents
目录,然后尝试使用NSFileManager -createFileAtPath
在那里创建一个文件。 Prior to iOS 8 this works fine. 在iOS 8之前,这可以正常工作。 On devices running iOS 8, I get the following on the call to
-createFileAtPath:
在运行iOS 8的设备上,我在调用
-createFileAtPath:
得到以下-createFileAtPath:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSFileManager fileSystemRepresentationWithPath:]: nil or empty path argument'
Here is a minimal code snippet that reproduces the problem: 这是一个重现问题的最小代码片段:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:docsDir];
[[NSFileManager defaultManager] createFileAtPath:@"temp.dat" contents:nil attributes:nil];
Note the path argument on createFileAtPath
is "temp.dat"
- if I change it to "./temp.dat"
, the call succeeds. 注意
createFileAtPath
上的path参数是"temp.dat"
- 如果我将其更改为"./temp.dat"
,则调用成功。
Am I doing something stupid and this was just "accidentally" working in prior iOS releases? 我做了一些愚蠢的事情,这只是“偶然”在之前的iOS版本中工作了吗? Or did they intentionally change the behavior here?
或者他们是否有意改变了这里的行为? Or is this a bug in iOS 8?
或者这是iOS 8中的错误? Other NSFileManager methods that take a
path
argument seem to be okay with just a filename (eg -removeItemAtPath:@"temp.dat" error:&err
succeeds). 采用
path
参数的其他NSFileManager方法似乎没有文件名(例如-removeItemAtPath:@"temp.dat" error:&err
成功)。
Edited to add: 编辑添加:
This only happens on a physical device. 这仅发生在物理设备上。 In the simulator, the above call to
createFileAtPath
succeeds. 在模拟器中,上面对
createFileAtPath
调用成功。
I opened a bug with Apple and they closed it as a duplicate. 我向Apple公开了一个错误,他们将其作为副本关闭。 While that doesn't necessarily confirm that they consider it a bug, it at least confirms that I'm not the first to encounter this, and that the behavior did indeed change with iOS 8.
虽然这并不一定证实他们认为这是一个错误,但它至少证实了我不是第一个遇到这个错误的人,并且这种行为确实在iOS 8中发生了变化。
For now, the solution is to prepend ./
to the filename, or supply an absolute path. 目前,解决方案是将
./
到文件名,或提供绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.