开发者

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 contains NO. If path doesn’t exist, the return value is undefined. Pass NULL 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜