views:

80

answers:

2

Hey you lot (again), I was wondering if anyone knew a fast and easy way to see if a file (namely a textfile), exists inside the App folder?

And how to handle an Error correctly if the file doesn't exist.

Help would be grand! :) ty!

A: 

You can check for file existence with something like:

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:aPath]) { ... }

Depending on what you're looking for, "aPath" might be something like:

NSString *aPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Sample.txt"];

or

NSString *aPath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"];
David Gelhar
+1  A: 

I don't have a Mac in front of me right now, but I believe you want to do something similar to this:


NSBundle *myBundle = [NSBundle mainBundle];
NSString *pathToFile = [myBundle pathForResource:@"MyImage" ofType:@"jpg"];
if (pathToFile != nil) {
    NSLog(@"MyImage.jpg found in the App bundle.");
}

See http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html for more information.

jsumners
Sorry it took me forever to accept an answer!
Neurofluxation