IOS uses plist and archive to store data

Source: Internet
Author: User

1 storing data using plist files

The first thing to know is that using plist to store data, you can only store the data dictionary and array of OC, can not store the custom data model, example look at the style of the info.plist know

Storing data in a plist file

Gets the local sandbox path
    nsarray *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
    Gets the full path
    nsstring *docpath = [path objectatindex:0];
    3. New data
    Nsmutablearray *data = [[Nsmutablearray alloc] initwithobjects:@ "232", @ "Jee", nil];
    NSString *filepath = [Docpath stringbyappendingpathcomponent:@ "Data.plist"];
    NSString *str = @ "21323";
    [Data insertobject:str atindex:0];
    [Data Writetofile:filepath Atomically:yes];


Read and modify plist files

Gets the local sandbox path
    nsarray *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
    Gets the full path
    nsstring *docpath = [path objectatindex:0];
    3. File path
    nsstring *filepath = [Docpath stringbyappendingpathcomponent:@ "Data.plist"];
    
    4. Read Data
    nsmutablearray *data = [Nsmutablearray arraywithcontentsoffile:filepath];
    5. Modify the data
    nsstring *str = @ "wwwww";
    [Data insertobject:str atindex:0];
    [Data Writetofile:filepath Atomically:yes];
    NSLog (@ "%@", data);



2. storing Data Using Archives

With archived storage, you can store custom model data.

First let model follow the Nscoding protocol

#import <Foundation/Foundation.h>

@interface subdata:nsobject<nscoding>
@property (copy, nonatomic) NSString * name;
@property (copy, nonatomic) NSString * code;
@property (copy, nonatomic) NSString * IMGURL;
@property (copy, nonatomic) NSString * SUB;
@property (copy, nonatomic) nsstring * URL;

@end

And then in the M file,

This method is invoked when a custom object is saved to a file///
in the method that describes how to store the properties of the custom object
//Also say in the method it is clear which properties of the custom object are stored
-(void) Encodewithcoder :(Nscoder *) acoder
{
    [acoder encodeObject:self.name forkey:@ ' name '];
    [Acoder encodeObject:self.code forkey:@ "code"];
    [Acoder encodeObject:self.imgUrl forkey:@ "Imgurl"];
    [Acoder encodeObject:self.sub forkey:@ "Sub"];
    [Acoder encodeObject:self.url forkey:@ "url"];
}

This method is invoked when an object is read from a file///
In this method that shows how to read an object saved in a file
//That is to say in the method how to read the object in the file
-(ID) Initwithcoder: ( Nscoder *) Adecoder
{
    //Note: The method in which the parent class needs to be initialized first
    (Self=[super init]) {
        Self.name=[adecoder decodeobjectforkey:@ "name"];
        Self.code=[adecoder decodeobjectforkey:@ "Code"];
        Self.imgurl=[adecoder decodeobjectforkey:@ "Imgurl"];
        Self.sub=[adecoder decodeobjectforkey:@ "Sub"];
        Self.url=[adecoder decodeobjectforkey:@ "url"];
    return self;
}

So the setup in model is complete, followed by the code in use

Storing data in a file

Get file path
        nsstring *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) Lastobject];
        NSString *path=[docpath stringbyappendingpathcomponent:@ "Recentlyused.archiver"];
        Nsmutablearray *subdataarray = [[Nsmutablearray alloc] init];
        
        [Subdataarray Insertobject:model atindex:0];
        Saves a custom object to a file, calling the Nskeyedarchiver factory method Archiverootobject:tofile: Method
        [Nskeyedarchiver archiverootobject: Subdataarray Tofile:path];

Solution file

1. Get file path
    nsstring *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) Lastobject];
    NSString *path=[docpath stringbyappendingpathcomponent:@ "Recentlyused.archiver"];
    
    2. Read the object from the file, the Solution object will call a Nskeyedunarchiver factory method Unarchiveobjectwithfile: You can.
    if ([Gygeneraltools isfileexist:@ "Recentlyused.archiver"]) {
        Self.recentlyusedarray = [Nskeyedunarchiver Unarchiveobjectwithfile:path];
    }



Related Article

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.