Skip to content

Instantly share code, notes, and snippets.

@mthongvanh
Last active August 29, 2015 14:20
Show Gist options
  • Save mthongvanh/3156dc54982cb3f445f3 to your computer and use it in GitHub Desktop.
Save mthongvanh/3156dc54982cb3f445f3 to your computer and use it in GitHub Desktop.
It's important NOT to assume that NSString paths will always work if you construct them yourself. This bit me today. As it has many other times in the past.
// In both versions, we're feeding a string to the NSFileManager method, but only the second one succeeds. In the debugger console, they both appeared with the file:// so it was a bit difficult to debug. Of course, I'd momentarily forgot about every. other. time. it has happened.
// Version 1
- (BOOL)fileExistsInCaches:(NSString *)filename
{
NSString *path = [NSString stringWithFormat:@"%@%@",[self cacheDirectory],filename];
// path = file://Users/mthongvanh/etc/etc/etc
return [[NSFileManager defaultManager] fileExistsAtPath:path];
}
// Version 2
- (BOOL)fileExistsInCaches:(NSString *)filename
{
NSString *path = [NSString stringWithFormat:@"%@%@",[self cacheDirectory],filename];
NSURL *pathURL = [NSURL URLWithString:path];
// pathURL = /Users/mthongvanh/etc/etc
return [[NSFileManager defaultManager] fileExistsAtPath:pathURL.path];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment