IOS开发——自定义类归档(继承于自定义类)

2024-08-20 23:58

本文主要是介绍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开发——自定义类归档(继承于自定义类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091546

相关文章

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis