Data Storage Value Archive Archive

Source: Internet
Author: User

About archiving is a way of storing data in the Shu ' J data store in iOS. Let's look at an example in the archive:

The following is the parent class Person#import <Foundation/Foundation.h> @interface person:nsobject <NSCoding> @property (nonatomic , copy) NSString *name; @property (nonatomic,assign) int age; @property (nonatomic,assign) BOOL sex; @property (nonatomic, assign) float height; @property (nonatomic,assign) double weight; @end #import "Person.h" @implementation person//there must be , so save is to fetch-(ID) Initwithcoder: (Nscoder *) adecoder{        self = [Super init];         if (self) {        NSLog (@ "exists");         self.name =   [adecoder decodeobjectforkey:@ "name"];         self.age = [Adecoder decodeint32forkey:@ "Age"];        Self.sex = [Adecoder decodeboolforkey:@ "Sex"];        self.height = [ADecoder decodefloatforkey:@ "height"];        self.weight =[Adecoder decodedoubleforkey:@ "Weight"];   }    return self;} -(void) Encodewithcoder: (Nscoder *) acoder{   //There are documents that can be seen encoded in a way coded     [Acoder EncodeObject:self.name forkey:@ "Name"];    [Acoder encodeInt:self.age forkey:@ "age"];     [Acoder encodeBool:self.sex forkey:@ "Sex"];    [acoder encodeFloat:self.height forkey:@ "Height "];    [Acoder encodeDouble:self.weight forkey:@" Weight "];} @end

Subclass Student inherits the Person class: #import <Foundation/Foundation.h> #include "Person.h" @interface Student:person@property ( nonatomic,copy) NSString * content; @property (nonatomic,assign) float grade; @end #import "Student.h" @implementation student//there must be a fetch, so save is to take-(ID) Initwithcoder: (Nscoder *) adecoder{    if (self = [Super Initwithcoder:adecoder]) {        NSLog (@ "exists");        Self.content =   [adecoder decodeobjectforkey:@ "content"];        Self.grade = [Adecoder decodefloatforkey:@ "grade"];            }    return self;} -(void) Encodewithcoder: (Nscoder *) acoder{        [Super Encodewithcoder:acoder];    There are documents that can be seen encoded in a way encoded    [Acoder encodeObject:self.content forkey:@ "content"];    [Acoder encodeFloat:self.grade forkey:@ "grade"];} @end

The following is an archive of data in the Viewcontroller with two buttons

#import "ViewController.h" #import "Person.h" #import "Student.h" @interface Viewcontroller ()//two buttons that were saved and retrieved by the chamber-( ibaction) Savearchive: (ID) sender;-(ibaction) obtainunarchive: (ID) sender; @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];}    Save data-(Ibaction) Savearchive: (ID) Sender {NSLog (@ "Start encoding Archive and save");  Archive requires elements: 1, Save object 2, save file directory 3, save Manager (Archiver)//Get the object to be saved, object must be naïve and so on/person *person = [Person alloc]init];//person.age = 18;//Person.name = @ "Linyu";//person.sex = YES;        expressed as a male//person.height = 180;//person.weight = 60;    Student *stu = [[Student alloc]init];    Stu.age = 18;    Stu.name = @ "Linyu";    Stu.sex = YES;    Stu.height = 180;    Stu.weight = 60;    Stu.content = @ "I am a student!";    Stu.grade = 98.9; Get the path to save the file nsstring *documentdir = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES    ) Lastobject];    NSString * dir = [documentdir stringbyappendingstring:@ "Doc.txt"]; NSLog (@ "%@", dir); [Nskeyedarchiver archiverootobject:stu tofile:dir];}    Get Data-(ibaction) Obtainunarchive: (ID) Sender {//Get archived content, NSLog (@ "Get content"); NSString *documentdir = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) LastObject]    ;    NSString * dir = [documentdir stringbyappendingstring:@ "Doc.txt"];    Student *per = [Nskeyedunarchiver unarchiveobjectwithfile:dir];    NSLog (@ "%@", dir); NSLog (@ "%@%d%d%.2f%.2f%.2f%@", per.name,per.age,per.sex,per.height,per.weight,per.grade,per.content);} @end

Results of the output:


Summarize:

We can know file archives:
1, numbering set (preferrence) Nsuserdefault: This is actually through the plist file to store, but when the inside has been encapsulated in the later stage above. stored in the Preferrence file.
2, plist file, through their own path to obtain the file (and create), the data stored in the inside, which is generally in the Dorectory directory below.

3, Archives: Archived data storage is compressed, so the display is not clear-text storage, and archiving is used to store objects.

4. Note that the class to be stored follows the Nscoding protocol.


The four ways in which data is stored can only be stored in the corresponding basic data type. and store it in a way that generates an archive. (objects can be stored)



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data Storage Value Archive Archive

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.