ios中 继承对象模型的归档实现
2015-06-24 15:00
399 查看
之前项目中使用到了归档的技术,也用到了MJExtension
但是问题是,这个公共库遇到了无法归档的一些问题,让人蛋疼不已,怎么办呢。
对于不能归档的部分,职能手动归档,很是无语。
查找了一下原因:
原来对于两个模型,如何A继承了B,那么A有很大的情况是无法归档的!
自己写了。
对于上述的A模型和B模来说,定义如下:
它的归档要写成如下形式:
B模型定义如下:
它的归档则要写成如下的形式:
以上两个类,子类要调用父类的 initwithCoder方法
否则负类中的属性就无法被归档
但是问题是,这个公共库遇到了无法归档的一些问题,让人蛋疼不已,怎么办呢。
对于不能归档的部分,职能手动归档,很是无语。
查找了一下原因:
原来对于两个模型,如何A继承了B,那么A有很大的情况是无法归档的!
自己写了。
对于上述的A模型和B模来说,定义如下:
#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
它的归档要写成如下形式:
- (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; }
B模型定义如下:
#import "Coder.h" @interface CoderChild : Coder @property (nonatomic, strong) NSString *king; @property (nonatomic, strong) NSString *father; @end
它的归档则要写成如下的形式:
- (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; }
以上两个类,子类要调用父类的 initwithCoder方法
否则负类中的属性就无法被归档
相关文章推荐
- 如何在iOS中使用ZXing库
- iOS开发:设计模式那点事
- iOS中block实现的探究
- ios 断点续传注意事项
- iOS 开发学习36 reveal使用
- iOS——二维码扫描
- iOS 9适配系列教程:后台定位
- 百度地图iOS SDK v2.8.0 地图不显示 只显示表格解决办法
- 在iOS开发中常用的参数传递有以下几种方法:
- [IOS]JASidePanels(侧滑栏)的使用
- ios打包IPA的各种问题和解决方法
- iOS9地图增公共交通导航功能 中国300个城市可用
- iOS在地图上画图和导航,同时设定比例
- iOS[问题解决] Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed.
- NSArray ,NSSet, NSDictionary
- iOS在地图上画图和导航
- 深度解析iOS应用程序的生命周期
- ios播放本地声音文件
- 斯坦福大学公开课:iOS8开发 第一课:课务,iOS8概述学习笔记
- iostat 等运维命令用法