views:

504

answers:

1

Hello everyone

I have this code below:

NSString *fileName = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentDownload"];
    NSString *fullPath = [NSBundle pathForResource:fileName ofType:@"txt" inDirectory:[NSHomeDirectory() stringByAppendingString:@"/Documents/"]];
    NSError *error = nil;

    [textViewerDownload setText:[NSString stringWithContentsOfFile:fullPath encoding: NSUTF8StringEncoding error:&error]];

textviewerdownload is the textview displaying the text from the file. The actual file name is stored in an NSUserDefault called recentDownload.

When I build this, I click the button which this is under, and my application crashes.

Is there anything wrong with the syntax or just simple error?

+1  A: 

The NSBundle class is used for finding things within your applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work. Try this instead:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);

NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"]; 
jlehr
Ok I changed some of the coding.. My application still crashes.. Here is the code under my IBAction: http://www.heliotop.org/code.rtfAlso how would I do an if statement, where if the recent file is not a file with the extension "txt", it would return, and if it was then result in an action..
Kevin