fileExistsAtPath API always return NO
Following is my code I need check whether file present in document directory ,But following API does not return true at any condition but file is get created in document directory. Please 开发者_如何转开发check any thing wrong I am doing. I have searched on this other says this API "fileExistsAtPath" should work but it is not working in my case. Please do help me.
NSString *recordFile=[NSString stringWithFormat:@"MyFile.acc",data.uid];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentDr = [documentPaths objectAtIndex:0]; // WHY 0 ?
NSString *uniquePath = [documentDr stringByAppendingPathComponent:recordFile];
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
NSLog(@"File found")
}
else {
NSLog(@"No File to play");
}
Thanks, Sony
Probably your path is incorrect. What should be the name of the files you are trying to find? If you have @"MyFile1.acc"
, @"MyFile2.acc"
etc (where 1, 2 is the data.uid
value) then try to use this:
NSString *recordFile=[NSString stringWithFormat:@"MyFile%i.acc",data.uid];
Try to append a "/" after documentDr when define uniquePath, or
NSString *recordFile = [NSString stringWithFormat:@"MyFile%@", data.uid];
...
if([NSBundle pathForResource:recordFile ofType:@"acc" inDirectory:documentDr])
the first wrong thing i note is the first instruction. Why do you pass 'data.uid' as an argument to the format string ? Assuming 'data.uid' is some kind of integer, there should be at least '%d' in...
I had a look to Apple Documentation, in particular, that statement which says
The directory returned by this method may not exist.
You could try getting the current directory path using NSFileManager methods, and then testing for file existence (or create a directory at a place you can easily retrieve later).
What about the others paths ? You wrote 'WHY 0 ?', i'm asking the same question as you.Try looping over the array elements, testing each time for file existence.
The code should work. Your file most likely doesn't exist. Did you use capitalisation in your file name right when saving it? Also right now, I still don't know what the file name is since, as already said, the first line in your initial post doesn't give the format string right. Before checking try for existance try:
[[NSString stringWithString:@"test"] writeToFile: uniquePath atomically:YES];
That should create a file at the path and you see, that fileExists works.
I tried many things but ended up that I had to delete the app, clean all targets, and then rebuild again. It's weird that somehow my new resources doesn't get copied to the app. It usually happens to files that when I added them I selected "Create folder references for any added folders".
精彩评论