views:

145

answers:

2

I have been reading alot on iPhone read/writing and I believe that everything I have is correct but for some reason it isn't working the way it should.

code is as follows

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingFormat:@"filename.rtf"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath])

I went into the folder iphone simulator documents folder for the app I am making and added that file into it as a safety measure and it is still not triggering the if statement. So for some reason it is not finding it which is making all of my writing and reading impossible. I even tried using the FileManager's createFile function and didn't get any results. If I need to post more code of something let me know but I figure if all of the above is correct it should be finding the file correctly. Thanks in advance.

UPDATE: Fixed. Hilariously simple mistake. Thanks everyone.

A: 

Have you tried using stringByAppendingPathComponent: instead of stringByAppendingFormat:. If docsDirectory does not contain a trailing slash, you will actually be creating a file called /PATH/TO/Documentsfilename.rtf rather than /PATH/TO/Documents/filename.rtf.

Martin Gordon
+2  A: 

The NSString returned by NSSearchPathForDirectoriesInDomains does not have a trailing slash, thus the path you are building is incorrect. To append the filename to the use stringByAppendingPathComponent which will handle the path separaters correctly.

To create the filePath, try:

NSString *filePath =[docsDirector stringByAppendingPathComponent:@"filename.rtf"];
Jason Jenkins
Oh my god, that's unbelievable. lol Thank you very much.
Robert