Checking for a file in the kit

How can we check if a file is present in an external package loaded into the application or not before extracting it?

+2
ios iphone cocoa-touch nsbundle
Apr 04 2018-11-11T00:
source share
2 answers

If you only want to find out if there is, so you can decide whether to download it or not, I would just try to download it and check if the returned object is there. If so, you know that it does not exist in the kit.

+4
Apr 05 2018-11-11T00:
source share

To check if a file exists in the bundle, use the NSBundle class.

 NSString *path = [[NSBundle mainBundle] pathForResource:@"somefileinbundle" ofType:@"png"]; if (!path) NSLog(@"Unable to find file in bundle"); 

Having said that, as a rule, it is a bad idea to check if a file exists before trying to download it. According to Apple NSFileManager fileExistsAtPath: on NSFileManager fileExistsAtPath:

Attempting predicate behavior based on the current state of the file system or a specific file in the file system is not recommended. This can lead to odd behavior in case of race conditions of the file system. It is much better to try to perform an operation (for example, upload a file or create a directory), check for errors and process any error gracefully than try to figure out in advance whether the operation will be successful. For more information about the race condition of the file system, see "Preventing race conditions and unsafe file operations" in the Safe Encoding Guide.

+13
Sep 20 '11 at 14:52
source share



All Articles