views:

702

answers:

3

NSFilemanager is returning true for the following, when there should not be any such file there yet. What is happening?

    if([myManager fileExistsAtPath:[[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"]]){

NSLog(@"file is there");

}
A: 

Print this out and see if it is what you expect.

NSLog(@"Directory: %@", [[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"]];

Also, check that you are defining myManager correctly.

Jordan
+2  A: 

The documentation for NSFileManager seems to recommend not checking to see if files exist, and instead just trying to read the file and handle any errors gracefully (e.g. file not found error). What you are describing doesn't sound like a race condition—which is what the documentation's recommendation is trying to circumvent—but what happens if you just try to load the file rather than checking to see if it exists? You could, for example, try the following:

NSError *error;
NSStringEncoding encoding;
NSString *fileContents = [NSString stringWithContentsOfFile:fileName
                                               usedEncoding:&encoding
                                                      error:&error];

if (fileContents == nil)
{
    NSLog (@"%@", error);
}
else
{
    NSLog (@"%@", fileContents);
}

If you get a string with all of the file's contents, then the file is obviously there. If you get an error then something is up with myManager.

dreamlax
A: 

This works as expected for me.

 NSFileManager *myManager = [NSFileManager defaultManager];
NSString *documentsDirectory = NSHomeDirectory();

if([myManager fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"Music/songlist.txt"]]){

    NSLog(@"file is there");

}
markhunte
P.s, Jordan, you have errors in your code:)
markhunte