Ios-plist file Storage (1)

Source: Internet
Author: User

1. What is a file system?

Each app in iOS has its own file system and access rights, which are generally divided into

~/documents/

~/tmp/

~/library/caches/

~/library/preferences/-------Key-value pairs without caring for file paths.

Its path is obtained in the form of

<span style= "color: #999999;" >{    //Get home directory    nsstring *path=nshomedirectory ();    NSString *docpath=[path stringbyappendingpathcomponent:@ "Documents"];    NSLog (@ "%@", DocPath);    Get file directory    Nsarray *documentpath=nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsalldomainsmask, YES );    NSLog (@ "%@", Documentpath[0]);    Get Cache directory    Nsarray *cachepath=nssearchpathfordirectoriesindomains (nscachesdirectory, Nsalldomainsmask, YES);    NSLog (@ "%@", Cachepath[0]);    Get temp directory    nsstring *temp=nstemporarydirectory ();    NSLog (@ "%@", temp);} </span>


plist files can only store content nsstring nsnumber nsdata Nsarray nsdictionary, and their files are stored in XML format

Nsarray stored in documents:

    Nsarray *[email protected][@ "name" @ "age" @ "height"];    NSString *path=nshomedirectory ();    NSString *docpath=[path stringbyappendingpathcomponent:@ "Documents"];    NSString *filepath=[docpath stringbyappendingpathcomponent:@ "/aa.plist"];    Store the array in the plist file    [arr writetofile:filepath atomically:yes];    Read from file path as array    nsarray *arr2=[nsarray Arraywithcontentsoffile:filepath];

Nsdictionary stored in the cache:

Nsdictionary *di[email protected]{@ "name": @ "lean", @ "age": @24,@ "height": @172};    Nsarray *dicarr=nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsalldomainsmask, YES);    NSLog (@ "%@", Dicarr[0]);    NSString *dirpath=dicarr[0];    NSString *filepath=[dirpath stringbyappendingpathcomponent:@ "Dic.plist"];    Store the dictionary in the plist file    [dic Writetofile:filepath Atomically:yes];    Read from file path as dictionary    nsdictionary *dic2=[nsdictionary Dictionarywithcontentsoffile:filepath];

NSData Reading Pictures:

Reading and writing pictures can be stored directly only through NSData.    //The following example stores a file from Uiimageview and reads the display    in another control Nsarray *arr=nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsalldomainsmask, YES);    NSLog (@ "%@", Arr[0]);    NSString *cachepath=arr[0];    NSString *filepath=[cachepath stringbyappendingpathcomponent:@ "Image.plist"];    UIImage *IMAGE=[SELF.A image];    NSData *data=uiimagejpegrepresentation (image,1);    [Data Writetofile:filepath Atomically:yes];        NSData *data2=[nsdata Datawithcontentsoffile:filepath];    UIImage *image2=[uiimage Imagewithdata:data2];    Self.b.image=image2;





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.