Getting and manipulating the IOS file directory

Source: Internet
Author: User
Tags tmp folder

The iphone sandbox model has four folders, what is it, where is the permanent data store, and what is the simple way to get the path to the emulator. Documents,tmp,app,library. (Nshomedirectory ()), manually saved files in the documents file Nsuserdefaults saved files in the TMP folder
1, documents directory: Generally used to store files created in the application, such as database files, or the program to browse the file data. If you make a backup, the files in this folder are included. 2, Appname.app directory: This is the application's package directory, which contains the application itself. Because the application must be signed, you cannot modify the contents of this directory at run time, which may cause the application to fail to start. 3, Library directory: This directory has two subdirectories: Caches and Preferencespreferences Directories: Contains the application's preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set your application's preferences. Caches directory: Used to store application-specific support files to save the information that is needed during the application restart. 4, tmp directory: This directory is used to store temporary files, saving information that is not needed during the application restart. //ways to get these directory paths://function to get home directory path:NSString *homedir =nshomedirectory ();//ways to get the documents directory path:Nsarray *paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*docdir = [Paths Objectatindex:0];//to get the caches directory path:Nsarray *paths =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString*cachesdir = [Paths Objectatindex:0];//method to get the TMP directory path:NSString *tmpdir =nstemporarydirectory ();//to obtain the path to the resource file in the application package://For example, get a picture resource (apple.png) path in the package by:NSString *imagepath =[[NSBundle Mainbundle] pathforresource:@ "apple" oftype:@ "PNG"]; UIImage*appleimage =[[UIImage alloc] initwithcontentsoffile:imagepath];//the Mainbundle class method in the code is used to return an object that represents the application package. 1, Common Nsfilemanager file Methods-(NSData *) Contentsatpath:path//reading data from a file-(BOOL) Createfileatpath:path contents: (NSData *) Data attributes:attr//write data to a file-(BOOL) Removeitematpath:path Error:err//Delete a file-(BOOL) Moveitematpath:from topath:to Error:err//Rename or move a file (to cannot be existing)-(BOOL) Copyitematpath:from topath:to Error:err//copy files (to cannot be existing)-(BOOL) Contentsequalatpath:path andpath:path2//Compare the contents of two files-(BOOL) Fileexistatpath:path//test whether the file exists-(BOOL) Isreadablefileatpath:path//test whether the file exists and can perform read operations-(BOOL) Iswriteablefileatpath:path//test whether the file exists and can perform a write operation-(Nsdictionary *) Attributesofitematpath:path Error:err//get the properties of a file-(BOOL) setattributesofitematpath:attr Error:err//change the properties of a file2. Using the Catalog-(NSString *) Currentdirectorypath//Get current directory-(BOOL) Changecurrentdirectorypath:path//change the current directory-(BOOL) Copyitematpath:from topath:to Error:err//Copy directory structure (to cannot be existing)-(BOOL) Createdirectoryatpath:path withintermediatedirectories: (BOOL) flag attribute:attr//Create a new directory-(BOOL) Fileexistatpath:path isdirectory: (bool*) flag//test file is not a directory (flag stored results yes/no)-(Nsarray *) Contentsofdirectoryatpath:path Error:err//Listing Directory Contents-(Nsdirectoryenumerator *) Enumeratoratpath:path//enumerating the contents of a directory-(BOOL) Removeitematpath:path Error:err//Delete Empty directory-(BOOL) Moveitematpath:from topath:to Error:err//Rename or move a directory (to cannot be existing)3, Common Path tool method+ (NSString *) pathwithcomponens:components//construct a valid path based on elements in components-(Nsarray *) pathcomponents//a destructor path that gets the parts that make up this path-(NSString *) lastpathcomponent//extract the last component of a path-(NSString *) pathextension//extract its extension from the last component of the path-(NSString *) Stringbyappendingpathcomponent:path//add path to the end of an existing path-(NSString *) Stringbyappendingpathextension:ext//adds the specified extension to the last component of the path-(NSString *) stringbydeletinglastpathcomponent//Delete the last component of a path-(NSString *) stringbydeletingpathextension//remove the extension from the last part of the file-(NSString *) Stringbyexpandingtileinpath//expands the path in the generation of characters to the user home directory (~) or the specified user's home directory (~user)-(NSString *) Stringbyresolvingsymlinksinpath//attempt to parse a symbolic link in a path-(NSString *) Stringbystandardizingpath//by trying to parse ~ 、.. (parent directory symbol),. (current directory symbol), and symbolic link to standardize the path4, the Common path tool function NSString* Nsusername (void)//returns the login name of the current userNSString* Nsfullusername (void)//returns the full user name of the current userNSString* Nshomedirectory (void)//returns the path of the current user home directoryNSString* Nshomedirectoryforuser (nsstring* user)//returns the user's home directoryNSString* Nstemporarydirectory (void)//returns the path directory that can be used to create temporary files5, Common iOS directory documents (nsdocumentdirectory)//A directory for writing application-related data files, files written here in iOS can be shared with itunes and accessed, and files stored here are automatically backed up to the cloudLibrary/caches (Nscachesdirectory)//The directory used to write application support files, saving the information that the application needs to start again. itunes does not back up the contents of this directorytmp (use Nstemporarydirectory ())//This directory is used to store temporary files, which need to be removed when the program terminates, and should be removed from this directory when the application no longer needs these temporary files.Library/preferences//This directory contains the application's preferences file, using the Nsuserdefault class to create, read, and modify the preferences file

Getting and manipulating the IOS file directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.