I'm having trouble with fileExistsAtPath:isDirectory
In my app the user can select an image with a popup dialogue. The first time they do so everything works as expected, and during the addition of the image fileExist开发者_如何学编程sAtPath:isDirectory is called and dir returns NO. However, when the user selects an image the second time fileExistsAtPath:isDirectory always returns YES (even if it is NOT a directory).
-(void) addImagesWithPath:(NSURL *)fileURL recursive:(BOOL) recursive{
int i, n;
BOOL dir;
NSError *error;
NSURL *newURL;
[[NSFileManager defaultManager] fileExistsAtPath:[fileURL absoluteString] isDirectory:&dir];
EDIT: if I add "dir = NO" before the method call it seems to work, but it feels like a hack.
You should always check the return value of -[NSFileManager fileExistsAtPath:isDirectory:]
prior to inspecting the contents of the second (output) parameter. The output parameter is only meaningful if the method returns YES
. This is described in the documentation:
Upon return, contains
YES
if path is a directory or if the final path element is a symbolic link that points to a directory, otherwise containsNO
. If path doesn’t exist, the return value is undefined. PassNULL
if you do not need this information.
If the method returns NO
then the path doesn’t exist or your application cannot access it. If it does exist, check whether your path is a full path (for instance, it shouldn’t start with ~
).
Also, you should be using -[NSURL path]
instead of -[NSURL absoluteString]
when passing paths to NSFileManager
.
精彩评论