An archive implementation of the inherited object model in iOS

Source: Internet
Author: User


Prior to the use of archived technology in the project, also used in the Mjextension

But the problem is, this public library encountered some problems can not be archived, so that the egg pain, how to do it.

For the parts that cannot be archived, the function is manually archived, very silent.


Find out why:

Originally for two models, how a inherits B, then A has a big situation is unable to archive!


Wrote it myself.

For the A model and the B-mode above, the definition is as follows:

#import <Foundation/Foundation.h> #import "Student.h" @interface Coder:nsobject@property (nonatomic,copy) NSString *text; @property (nonatomic,copy) nsstring *username; @property (nonatomic,copy) nsstring *classid; @property ( Nonatomic,strong) Student *stu; @end


Its archive should be written in the following form:

-(void) Encodewithcoder: (Nscoder *) acoder{    [Acoder encodeobject:_classid forkey:@ "ClassId"];    [Acoder encodeobject:_username forkey:@ "UserName"];    [Acoder encodeobject:_text forkey:@ "text"];    [Acoder encodeobject:_stu forkey:@ "Stu"];    } -(ID) Initwithcoder: (Nscoder *) Adecoder//ns_designated_initializer{    _classid = [Adecoder decodeobjectforkey:@] ClassId "];    _username = [Adecoder decodeobjectforkey:@ "UserName"];    _text = [Adecoder decodeobjectforkey:@ "text"];    _stu = [Adecoder decodeobjectforkey:@ "Stu"];        return self;}




The B model is defined as follows:

#import "Coder.h" @interface Coderchild:coder@property (nonatomic, strong) NSString *king; @property (nonatomic, Strong) NSString *father; @end

Its archive is written in the following form:

-(void) Encodewithcoder: (Nscoder *) acoder{    [Super Encodewithcoder:acoder];    [Acoder encodeobject:_king forkey:@ "King"];    [Acoder encodeobject:_father forkey:@ "father"];} -(ID) Initwithcoder: (Nscoder *) Adecoder//ns_designated_initializer{    unsigned int count = 0;    self = [super Initwithcoder:adecoder];    if (self) {        _king = [Adecoder decodeobjectforkey:@ "King"];        _father = [Adecoder decodeobjectforkey:@ "father"];    }    return self;}


Above two classes, subclass to invoke the Initwithcoder method of the parent class

Otherwise, the attributes in the negative class cannot be archived.




An archive implementation of the inherited object model in iOS

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.