NSFileManager -createFileAtPath fails NSInvalidArgumentException
Maybe I just missed something in the documentation, but I can't find anything that says this behavior should have changed in iOS 8.
My application sets the current working directory to a directory Documents
and then tries to create a file there with NSFileManager -createFileAtPath
. Before iOS 8, this works fine. On devices running iOS 8, I get the following when called-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 that the path argument createFileAtPath
is "temp.dat"
- , if I change it to "./temp.dat"
, the call will succeed.
Am I doing something stupid and was it just "accidental" working on previous versions of iOS? Or were they deliberately changing the behavior here? Or is this a bug in iOS 8? Other NSFileManager methods that take an argument path
look fine, just with a filename (for example, -removeItemAtPath:@"temp.dat" error:&err
successfully).
Edited to add:
This only happens on a physical device. In the simulator, the above call createFileAtPath
succeeds.
source to share
I opened a bug with Apple and they closed it as a duplicate. While that doesn't necessarily confirm that they think it's a bug, it does at least confirm that I'm not the first to run into this and that the behavior has indeed changed since iOS 8.
The current solution is to add ./
to the filename or specify an absolute path.
source to share