The path of the sandboxed storage

Source: Internet
Author: User
Tags string to file

The path of the sandboxed storage

Documents: Saves data that needs to be persisted when the application runs, and itunes automatically backs up the directory

Library: Stored program defaults and other status information, itunes automatically backs up the directory

Library/caches: Store cached files, itunes does not back up this directory, this directory file will not be deleted in app exit. General storage volume is relatively large, not particularly important resources

Library/preferences: Save all your app's preferences, itunes will automatically back up the directory

TMP: Saves temporary data that is required to run the app, deletes the file from the directory when it is finished, and itunes does not sync the directory

Address is a string

First parameter: enumeration value, enumerate the folder you want to find "to enter which folder directly modify its enumeration value can be"nsdocumentdirectory: Enter Documents folder

Second parameter:nsuserdomainmask represents the user's home directory

Third parameter: General set to YES means full path is shown

Nssearchpathfordirectoriesindomains Find the sandbox path, the return value is an array, the array is an element, according to the subscript out can be

The first way to find the path to documents

1 // Find the Documents path by function 2     NSString *documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0]; 3 // Print to confirm whether to get to path 4     NSLog (@ "documentpath =%@", documentpath);

The second method finds the path to the caches folder

1 //First step: Find the Home directory folder first2NSString *homepath =nshomedirectory ();3NSLog (@"HomePath =%@", HomePath);4     5 //Step Two: Then stitch the folder name you want to enter6NSString *cachespath = [HomePath stringbyappendingpathcomponent:@"library/caches"];7NSLog (@"Cachespath =%@", Cachespath);

Note: The path acquisition of TMP differs from other

1 // TEM has its own lookup function. 2     NSString *tmppath = nstemporarydirectory (); 3 // Print 4     NSLog (@ "tmppath =%@", Tmppath);

Local persistence for simple objects

1 #pragmaMark-Local persistence for simple objects2 #pragmaMark-Save data of type NSString to local3 4     //1> need to know where this object exists, so a path to a folder is required5     //Find Documents folder path6     7NSString *documentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0];8     9     //2> We know what to store, so what we need to createTen     //Create the content to store: string One      ANSString *str =@"AJAR"; -      -     //3> need to know where the string is ultimately stored, so you need to create a path to store the string theNSString *strpath = [Documentspath stringbyappendingpathcomponent:@"Wmw.txt"]; -      -     //4> ready to complete, write string to file -     //first parameter: a path to the file +     //second parameter: Automatic saving of power off -     //third parameter: encoding format +      A [str writetofile:strpath atomically:yes encoding:nsutf8stringencoding Error:nil]; at      - #pragmaMark-Take out the contents of the NSString folder store -     //Remove the string: Use the Stringwithcontentsoffile: method to remove it -     //first parameter: The path of the string store -     //second argument: writing a format -     //Third parameter: Error message inNSString *newstr =[NSString Stringwithcontentsoffile:strpath encoding:nsutf8stringencoding Error:nil]; -      toNSLog (@"%@", NEWSTR);

Nsstring,nsarray,nsdictionary The local persistence method of these objects is basically the same, but the steps to create the stored content are different, not the one by one example, the following is said NSData

1 #pragmaMark-store data of type NSData to local (take picture for example)2     //Two common ways to initialize an image3     //UIImage *image = [UIImage imagenamed:<# (nonnull nsstring *) #>];4     //UIImage *image = [[UIImage alloc] initwithcontentsoffile:<# (nonnull nsstring *) #>];5     6     //1, imagenamed the first time to read, first put the picture in the cache, the second time in the use of the same name when the picture is read directly from the cache (advantages: Convenient and fast, only the first time to use a little slower. Cons: If the current project is only used once will waste memory)7     //2, Initwithcontentsoffile initialization of the picture, each time will be based on the path to read, will not occupy memory, if the picture in the current project only used once, should choose this method8     9UIImage *image = [UIImage imagenamed:@"v_red_heart_selected"];Ten      One     /* A 123.png - [email protected] - [email protected] the       - related content of picture adaptation -      */ -      +     //converts an object of type image to a nsdata type of data for storage -     //use UIImageJPEGRepresentation to convert a picture to a NSData type +     //first parameter: The image object to convert A     //second parameter: The compressed value of a picture at     //images larger than 2M in the iphone will automatically rotate 90 degrees, then compress -NSData *imagedata = uiimagejpegrepresentation (image,1); -     //find path to store -NSString *documentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0]; -     //Final Path -NSString *imagepath = [Documentspath stringbyappendingstring:@"/123.jpeg"]; in     //Write - [ImageData Writetofile:imagepath atomically:yes]; toNSLog (@"%@", ImagePath); +      -     //reading NSData Types of data the     //requirement: NSData type of data is read out, converted to UIImage type and displayed on ImageView *NSData *newdata =[NSData Datawithcontentsoffile:imagepath]; $     Panax NotoginsengUIImage *showimage =[[UIImage alloc] initwithdata:newdata]; -      theUiimageview *imageview =[[Uiimageview alloc] initwithimage:showimage]; +[Self.view Addsubview:imageview];

Complex data localization

1     //How to store a person object locally, that is, for the localization of complex objects, this object must comply with the Nscoding protocol and implement two methods of the Protocol2    //Create Person class3 4 #pragmaMark-Localization of complex objects5     6     //1 Find Documents folder directory7NSString *documentspath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0];8     //2 Creating an object to store9Person *person =[[Person alloc] init];TenPerson.name =@"Mbboy"; OnePerson.age = About; APerson.gender =@"F"; -      -     //3 Archive This complex object the     //3.1 Initialize Nsmutabledata, which is used to create an archive tool -Nsmutabledata *data =[Nsmutabledata data]; -     //3.2 Creating an archive tool -Nskeyedarchiver *archiver =[[Nskeyedarchiver alloc] initforwritingwithmutabledata:data]; +     //3.3 Archive The person object to be archived -[Archiver Encodeobject:person Forkey:@" Person"]; +NSLog (@"==== %@", data); A     //3.4 End Archive at [Archiver finishencoding]; -      -NSLog (@"==== %@", data); -     //4 Store archived content nsmutabledata on-premises -NSString *personpath = [Documentspath stringbyappendingpathcomponent:@"person.plist"]; - [Data Writetofile:personpath atomically:yes]; inNSLog (@"==== %@", data); -      to      + #pragmaMark-de-document -     //1 data to be archived find out theNSData *resultdata =[NSData Datawithcontentsoffile:personpath]; *NSLog (@"%@", resultdata); $     //2 Creating a tool for unlocking filesPanax NotoginsengNskeyedunarchiver *unarchiver =[[Nskeyedunarchiver alloc] initforreadingwithdata:resultdata]; -     //3 Document the object to be unpacked (received using the Oerson object) thePerson *newperson = [Unarchiver decodeobjectforkey:@" Person"]; +     //4 Closing the solution file A [Unarchiver finishdecoding]; theNSLog (@"name =%@ Age =%ld gender =%@", Newperson.name, Newperson.age, Newperson.gender);

The path of the sandboxed storage

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.