Local storage with plist files

Source: Internet
Author: User

1, the need to save the information has been saved in plist file form on the local, that is, write to the sandbox:

/** *  write to local (plist file) */-(void) savearray{    //1. Get the sandbox root path    nsstring *home = Nshomedirectory ();        2.document path    nsstring *docpath = [Home stringbyappendingpathcomponent:@ "Documents"];        3. New data    nsdictionary *dict = @{@ "haha": @ "one",                           @ "hehe": @ "",                           @ "Huhu": @ "@" @ "                           Gogo": @                           "+"} ;        NSString *filepath = [DocPath stringbyappendingpathcomponent:@ "Data.plist"];            [Dict Writetofile:filepath atomically:yes];}

Where decuments is the sandbox fixed path, data.plist is to take the name of the file to be saved;

2, when need to take out the saved information, read the plist file saved in the Local:

/** *  Read Write local plist file */-(ibaction) Read {    //1. Get sandbox root path    nsstring *home = Nshomedirectory ();        2.document path    nsstring *docpath = [Home stringbyappendingpathcomponent:@ "Documents"];        3. File path    nsstring *filepath = [DocPath stringbyappendingpathcomponent:@ "Data.plist"];        4. Read Data    nsdictionary *dic = [Nsdictionary Dictionarywithcontentsoffile:filepath];        NSLog (@ "%@", DIC);}

  

3, sometimes saved in the local file information is not up-to-date, you need to delete the last saved in the local file, and then re-save the latest information on the Local:

/** *  Delete data stored in the local plist file */-(void) remove{    //1. Get the sandbox root path    nsstring *home = Nshomedirectory ();        2.document path    nsstring *docpath = [Home stringbyappendingpathcomponent:@ "Documents"];        3. File path    nsstring *filepath = [DocPath stringbyappendingpathcomponent:@ "Data.plist"];        Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];        [FileManager Removeitematpath:filepath error:nil];}

This completes the local storage of the file.

For small file storage, reduce the trouble with database storage, to plist file storage, light, fast, easy to operate.

Sometimes for the network data cache, if the data is not very dozen, but also can be stored with the plist file.

Local storage with plist files

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.