iOS开发- NSCoding协议,NSKeyedArchiver自定义类归
来源:互联网 发布:上海社会科学院 知乎 编辑:程序博客网 时间:2024/06/11 00:12
使用NSCoding协议可以实现归档自定义的类,NSKeyedArchiver可以归档我们自定义的类;要实现自定义类的归档,需要实现
我们可以直接在appDelegate中测试一下,如何 归档 和 读取 归档的数据:
encodeWithCoder(编码)和initWithCoder(解码)
我创建一个自定义的Student类,遵循NSCoding协议,实现这两个方法:
//// Student.h// UserList//// Created by http://blog.csdn.net/yangbingbinga on 14/11/14.// Copyright (c) 2014年 http://blog.csdn.net/yangbingbinga. All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject<NSCoding>@property(nonatomic,strong)NSString * name;@property(nonatomic,strong)NSString * age;@end.m文件
//// Student.m// UserList//// Created by yb on 14/11/14.// Copyright (c) 2014年 http://blog.csdn.net/yangbingbinga. All rights reserved.//#import "Student.h"@implementation Student- (void)encodeWithCoder:(NSCoder *)aCoder{ NSLog(@"%s",__FUNCTION__); [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeObject:self.age forKey:@"age"]; }- (id)initWithCoder:(NSCoder *)aDecoder{ NSLog(@"%s",__FUNCTION__); self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeObjectForKey:@"age"]; return self;}@end
我们可以直接在appDelegate中测试一下,如何 归档 和 读取 归档的数据:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ Student * stu = [[Student alloc]init]; stu.name = @"123"; stu.age = @"3"; NSData * stuD = [NSKeyedArchiver archivedDataWithRootObject:stu];//归档,调用encodeWithCoder方法 Student * stu1 = [NSKeyedUnarchiver unarchiveObjectWithData:stuD];//读取归档数据,调用initWithCoder NSLog(@"stu1.name = %@",stu1.name); return YES;}原文地址:http://blog.csdn.net/yangbingbinga
0 0
- iOS开发- NSCoding协议,NSKeyedArchiver自定义类归
- iOS 归档(NSKeyedArchiver + NSCoding)
- NSCoding / NSKeyedArchiver
- NSCoding/NSKeyedArchiver
- iOS自定义类 - NSKeyedArchiver归档自定义类
- iOS中NSCoding协议
- ios开发——NSCoding协议与NSUserDefaults相关
- iOS 数据存储 归档普通对象 NSCoding NSKeyedArchiver
- 实现数据模型缓存 可以用NSKeyedArchiver类来实现数据模型缓存。为了把模型对象用NSKeyedArchiver归档,模型类需要遵循NSCoding协议。
- 归档NSCoding、NSKeyedArchiver
- NSCoding NSKeyedArchiver NSKeyedUnarchiver
- 【ios】NSCoding协议的使用
- ios-day12-04(ios开发中数据存储之NSKeyedArchiver归档(NSCoding)、普通对象如何归档、普通对象的子类如何归档)
- iOS SDK详解之NSCoding协议
- iOS学习之——NSCoding协议
- NSCoding协议
- NSCoding协议
- NSCoding协议
- 新的一年Android学习计划
- node.js安装express后显示“express不是内部或外部命令”
- Find Minimum in Rotated Sorted Array II
- Fast-Start On-Demand Rollback&Fast-Start Parallel Rollback
- 【数据结构】顺序队列
- iOS开发- NSCoding协议,NSKeyedArchiver自定义类归
- Android开发这个怎么能少呢?
- git config配置文件
- C++11标准后的C++阅读书目
- 明明白白使用DES加密算法
- 仿微信、短信、QQ等消息数目右上角红色小圆球气泡显示(基于Android XML布局文件实现)
- Android Studio使用问题
- android ndk 调试
- 关于ios app名称注册的问题(抢注)