Checking for a file in the application folder

Hey you a lot (again), I was wondering if anyone knew a quick and easy way to see if there is a file in the application folder (namely a text file)?

And how to properly handle the error if the file does not exist.

Help will be great! :) ty!

+3
source share
2 answers

I believe that 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 https://developer.apple.com/documentation/foundation/nsbundle?language=objc for more details .

+5
source

You can check for a file with something like:

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

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

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

or

NSString *aPath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"];
+3

Source: https://habr.com/ru/post/1737346/


All Articles