Archiving Implementation of inherited object models in ios

Source: Internet
Author: User

Archiving Implementation of inherited object models in ios

 

Previously, archive technology was used in the project, and MJExtension was also used.

But the problem is that this public database has encountered some problems that cannot be archived, which makes it quite painful. What should I do.

For parts that cannot be archived, the function is manually archived, Which is speechless.

 

Find the cause:

It turns out that for two models, how does A inherit B? In this case, A cannot be archived!

 

Write it by yourself.

The preceding model A and model B are defined as follows:

 

#import 
 
  #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 as follows:

 

- (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;}


 

 

 

Model B is defined as follows:

 

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

Its archive format must be written as follows:

 

 

- (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;}


The subclass must call the initwithCoder method of the parent class.

 

Otherwise, attributes in the negative class cannot be archived.

 

 

 

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.