开发者

Checking if a file exists in the App folder

Hey you lot (again), I was wondering if anyone knew a fast and easy开发者_如何学JAVA 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!


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


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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜