本文主要是介绍IOS开发——自定义类归档(继承于自定义类),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
IOS开发——自定义类归档(继承于自定义类)
我先创建一个新闻基类,对其实现NSCoding协议;再创建一个图组新闻类继承于这个新闻基类,添加一些属性(图组),那么这个自定义的图组新闻类该如何实现归档呢?
下面是个痛苦的领悟...
图片新闻类:
#import "DMNewsDataModel.h"
@interface DMPictureNewsModel : DMNewsDataModel<NSCoding>
@property (strong, nonatomic)NSMutableArray *ymImageList;//图片列表
@end#import "DMPictureNewsModel.h"
@implementation DMPictureNewsModel
@synthesize ymImageList = _ymImageList;
//数据归档
- (void)encodeWithCoder:(NSCoder *)aCoder{[super encodeWithCoder:aCoder];[aCoder encodeObject:_ymImageList forKey:@"_ymImageList"];
}//数据逆归档
- (id)initWithCoder:(NSCoder *)aDecoder{self = [super initWithCoder:aDecoder];if(self){_ymImageList = [aDecoder decodeObjectForKey:@"_ymImageList"]; }return self;
}
@end
新闻基类:
#import <Foundation/Foundation.h>
@interface DMNewsDataModel : NSObject<NSCoding>
@property (strong, nonatomic)NSString *ymContentid; //新闻ID
@property (strong, nonatomic)NSString *ymTitle; //新闻标题
@property (strong, nonatomic)NSString *ymModelid; //"模型id"比如 1 文章 2 组图 3 视频
@property (strong, nonatomic)NSString *ymCatid; //分类id
@property (strong, nonatomic)NSString *ymDescription;//内容简介
@property (strong, nonatomic)NSString *ymPublished; //新闻发布时间戳
@property (strong, nonatomic)NSString *ymContent; //新闻内容
@property (strong, nonatomic)NSString *ymSource; //新闻来源
@property (strong, nonatomic)NSString *ymThumb; //新闻配图地址
@property (strong, nonatomic)NSString *ymVideo; //视频URL(请求的新闻模型为modelid=4,其他为空)
@property (strong, nonatomic)NSString *ymPlaytime; //点击次数
@property (strong, nonatomic)NSString *ymTopicid; // 话题id 用于评论
@property (strong, nonatomic)NSString *ymComments; //评论条数///新闻获取
-(void)setContentid:(NSString *)theContentidandTitle:(NSString *)theTitleandModelid:(NSString *)theModelidandCatid:(NSString *)theCatidandPublished:(NSString *)thePublishedandSource:(NSString *)theSourceandDescription:(NSString *)theDescriptionandContent:(NSString *)theContentandThumb:(NSString *)theThumbandVideo:(NSString *)theVideoandPlaytime:(NSString *)thePlaytimeandTopicid:(NSString *)theTopicidandComments:(NSString *)theComments;//归档,必须写前面
- (void)encodeWithCoder:(NSCoder *)aCoder;//逆归档
- (id)initWithCoder:(NSCoder *)aDecoder;
@end#import "DMNewsDataModel.h"
@implementation DMNewsDataModel
@synthesize ymContentid = _ymContentid;
@synthesize ymTitle = _ymTitle;
@synthesize ymModelid = _ymModelid;
@synthesize ymCatid = _ymCatid;
@synthesize ymPublished = _ymPublished;
@synthesize ymSource = _ymSource;
@synthesize ymDescription = _ymDescription;
@synthesize ymContent = _ymContent;
@synthesize ymThumb = _ymThumb;
@synthesize ymVideo = _ymVideo;
@synthesize ymPlaytime = _ymPlaytime;
@synthesize ymTopicid = _ymTopicid;
@synthesize ymComments = _ymComments;//归档
- (void)encodeWithCoder:(NSCoder *)aCoder{[aCoder encodeObject:_ymContentid forKey:@"_ymContentid"];[aCoder encodeObject:_ymTitle forKey:@"_ymTitle"];[aCoder encodeObject:_ymModelid forKey:@"_ymModelid"];[aCoder encodeObject:_ymCatid forKey:@"_ymCatid"];[aCoder encodeObject:_ymPublished forKey:@"_ymPublished"];[aCoder encodeObject:_ymSource forKey:@"_ymSource"];[aCoder encodeObject:_ymDescription forKey:@"_ymDescription"];[aCoder encodeObject:_ymContent forKey:@"_ymContent"];[aCoder encodeObject:_ymThumb forKey:@"_ymThumb"];[aCoder encodeObject:_ymVideo forKey:@"_ymVideo"];[aCoder encodeObject:_ymPlaytime forKey:@"_ymPlaytime"];[aCoder encodeObject:_ymTopicid forKey:@"_ymTopicid"];[aCoder encodeObject:_ymComments forKey:@"_ymComments"];}//逆归档
- (id)initWithCoder:(NSCoder *)aDecoder{self = [super init];if(self){_ymContentid = [aDecoder decodeObjectForKey:@"_ymContentid"];_ymTitle = [aDecoder decodeObjectForKey:@"_ymTitle"];_ymModelid = [aDecoder decodeObjectForKey:@"_ymModelid"];_ymCatid = [aDecoder decodeObjectForKey:@"_ymCatid"];_ymPublished = [aDecoder decodeObjectForKey:@"_ymPublished"];_ymSource = [aDecoder decodeObjectForKey:@"_ymSource"];_ymDescription = [aDecoder decodeObjectForKey:@"_ymDescription"];_ymContent = [aDecoder decodeObjectForKey:@"_ymContent"];_ymThumb = [aDecoder decodeObjectForKey:@"_ymThumb"];_ymVideo = [aDecoder decodeObjectForKey:@"_ymVideo"];_ymPlaytime = [aDecoder decodeObjectForKey:@"_ymPlaytime"];_ymTopicid = [aDecoder decodeObjectForKey:@"_ymTopicid"];_ymComments = [aDecoder decodeObjectForKey:@"_ymComments"];}return self;
}@end
这篇关于IOS开发——自定义类归档(继承于自定义类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!