[NSData dataWithContentsOfFile:path] doesn't work
- by Felics
Hello, when I have the fallowing code to read a binary file:
NSString* file = [NSString stringWithUTF8String:fileName];
NSString* filePath = resource ? [[NSBundle mainBundle] pathForResource:file ofType:nil] : [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent: file];
NSData* fileData = [NSData dataWithContentsOfFile:filePath];
Where "fileName" and resource are load function parameters. "resource" is used to know if the file is located in application bundle or in Documents. Sometimes this code works well and sometimes it doesn't. As far I saw this problem is random. I can run the code 10 times in a row and it works fine and after that it gives me nil data without any modification. Does anybody knows what could be the problem? Could it be related with file extension or file name?
Thank you.
PS: I use this code on iPhone Simulator and the file exists in application bundle.