Summary of basic methods for accessing and managing file directories in iOS development

  • 2020-05-12 06:15:21
  • OfStack

Access to file directories

Simplest :(because of the sandbox relationship, there is no folder concept)


UIImage* image = [UIImage imageNamed:@"11.png"];

This is already a packaged path relative to app, and no additional path is needed.


There is also a need to specify the file path:

You need NSBundle.


[[NSBundle mainBundle] resourcePath], This is the packaged path of the application.

If you need to specify a path, write:

You can also splice your own:


NSString* path = [NSStringstringWithFormat:@"%@/%@/%@",[[NSBundlemainBundle] resourcePath],@"document",@"aaa.txt"];

Or directly:

NSString* path = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"png"];

1 generally the application has three directories

Documents,Library,tmp

Currently, apple does not allow you to save big data in documents documents. If you want to save videos or anything in documents documents, you need to backup to icould.

The way to do this is to save it under the caches directory of Library (I don't know if that makes sense).

Save a few temporary files in the tmp directory, which you can remove from the cache when you exit the application.


Get your own documents directory in the application:


NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString * documentDirectory = [paths objectAtIndex:0];

Based on the above, get a complete file path and name:

NSString * file = [documentDirectory stringByAppendingPathComponent:@"file1.txt"];

This allows you to create, read, and write files using file.

File directory management
Here is a look at some common file directory management methods
1. Common NSFileManager file method


-(NSData *)contentsAtPath:path  // from 1 File to read data
-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr  // to 1 Two files write data
-(BOOL)removeItemAtPath:path error:err  // delete 1 A file
-(BOOL)moveItemAtPath : from toPath:to error:err  // Rename or move 1 A file ( to Can't be existing) -(BOOL)copyItemAtPath:from toPath:to error:err  // Copy file ( to Can't be existing)
-(BOOL)contentsEqualAtPath:path andPath:path2  // Compare the contents of the two files
-(BOOL)fileExistAtPath:path  // Test whether the file exists
-(BOOL)isReadableFileAtPath:path  // Test whether the file exists and can be read
-(BOOL)isWriteableFileAtPath:path  // Test whether the file exists and can be written
-(NSDictionary *)attributesOfItemAtPath:path error:err  // Gets the properties of the file
-(BOOL)setAttributesOfItemAtPath:attr error:err  // Change the properties of the file

2. Use a directory

-(NSString *)currentDirectoryPath  // Get the current directory
-(BOOL)changeCurrentDirectoryPath:path  // Change current directory
-(BOOL)copyItemAtPath:from toPath:to error:err  // Copy directory structure ( to Can't be existing)
-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr  // create 1 A new directory
-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag  // Test whether the file is a directory ( flag Medium stored result YES/NO )
-(NSArray *)contentsOfDirectoryAtPath:path error:err  // List contents
-(NSDirectoryEnumerator *)enumeratorAtPath:path  // Enumerate the contents of a directory
-(BOOL)removeItemAtPath:path error:err  // Delete empty directory
-(BOOL)moveItemAtPath:from toPath:to error:err   // Rename or move 1 A directory ( to Can't be existing)

3. Common path tool method

+(NSString *)pathWithComponens:components  // According to the components The element in the
-(NSArray *)pathComponents  // The path is destructed to get the parts that make up the path
-(NSString *)lastPathComponent  // Extract the end of the path 1 Component parts
-(NSString *)pathExtension  // From the end of the path 1 Extract its extension from the component
-(NSString *)stringByAppendingPathComponent:path  // will path Add to the end of the existing path
-(NSString *)stringByAppendingPathExtension:ext  // Adds the specified extension to the end of the path 1 Component parts -(NSString *)stringByDeletingLastPathComponent  // Delete the end of the path 1 Component parts
-(NSString *)stringByDeletingPathExtension  // From the end of the file 1 Partially delete the extension -(NSString *)stringByExpandingTileInPath   // Expand the path to the user's home directory ( ~ ) or the home directory of the specified user ( ~user ) -(NSString *)stringByresolvingSymlinksInPath  // Try to resolve symbolic links in the path -(NSString *)stringByStandardizingPath  // Parse by trial ~ , .. (parent directory symbol), . (current directory symbol) and symbolic links to standardize the path

4. Common path tool functions

NSString* NSUserName(void)  // Returns the login name of the current user
NSString* NSFullUserName(void)  // Returns the full user name of the current user
NSString* NSHomeDirectory(void)  // Returns the path to the current user's home directory
NSString* NSHomeDirectoryForUser(NSString* user)  // Back to the user user The home directory
NSString* NSTemporaryDirectory(void)  // Returns the path directory that can be used to create temporary files

5. Common IOS directory

[[NSBundle mainBundle] resourcePath], This is the packaged path of the application.
0


Related articles: