自定义类写入文件
2015-06-10 09:29
274 查看
//对自定义类归档
Person.h 遵守<NSCoding>协议
#import <Foundation/Foundation.h>
@class Book;
@interface Person : NSObject<NSCoding>
@property(copy,nonatomic)NSString *name;
@property(copy,nonatomic)NSString *sex;
@property(strong,nonatomic)Book *book;
@end
Person.m
#import "Person.h"
@implementation Person
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.sex forKey:@"sex"];
[aCoder encodeObject:self.book forKey:@"book"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
self.book = [aDecoder decodeObjectForKey:@"book"];
}
return self;
}
@end
----------------------写入文件-----------------
创建对象
Book *book1 = [[Book alloc] init];
book1.name = @"西游记";
//写入自定义类
Person *p1 = [[Person alloc] init];
Person *p2 = [[Person alloc] init];
Person *p3 = [[Person alloc] init];
p1.name = @"wyj1";
p1.sex = @"男1";
p1.book = book1;
用archive归档
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:p1 forKey:@"person"];
[archiver finishEncoding];
获取文件路径
NSString *doucment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
doucment = [doucment stringByAppendingString:@"/save"];
将data写入文件
[data writeToFile:doucment atomically:YES];
--------------自定义类对象从文件读取--------------
1.先获取路径
NSString *doucment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
doucment = [doucment stringByAppendingString:@"/save"];
NSData *data = [NSData dataWithContentsOfFile:doucment];
2.将NSData通过反归档,转化为Personz对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
3.通过反归档得到复杂对象
Person *p = [unarchiver decodeObjectForKey:@"person"];
[unarchiver finishDecoding];
Person.h 遵守<NSCoding>协议
#import <Foundation/Foundation.h>
@class Book;
@interface Person : NSObject<NSCoding>
@property(copy,nonatomic)NSString *name;
@property(copy,nonatomic)NSString *sex;
@property(strong,nonatomic)Book *book;
@end
Person.m
#import "Person.h"
@implementation Person
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.sex forKey:@"sex"];
[aCoder encodeObject:self.book forKey:@"book"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
self.book = [aDecoder decodeObjectForKey:@"book"];
}
return self;
}
@end
----------------------写入文件-----------------
创建对象
Book *book1 = [[Book alloc] init];
book1.name = @"西游记";
//写入自定义类
Person *p1 = [[Person alloc] init];
Person *p2 = [[Person alloc] init];
Person *p3 = [[Person alloc] init];
p1.name = @"wyj1";
p1.sex = @"男1";
p1.book = book1;
用archive归档
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:p1 forKey:@"person"];
[archiver finishEncoding];
获取文件路径
NSString *doucment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
doucment = [doucment stringByAppendingString:@"/save"];
将data写入文件
[data writeToFile:doucment atomically:YES];
--------------自定义类对象从文件读取--------------
1.先获取路径
NSString *doucment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
doucment = [doucment stringByAppendingString:@"/save"];
NSData *data = [NSData dataWithContentsOfFile:doucment];
2.将NSData通过反归档,转化为Personz对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
3.通过反归档得到复杂对象
Person *p = [unarchiver decodeObjectForKey:@"person"];
[unarchiver finishDecoding];
相关文章推荐
- 福建省漳州市医院信息系统备份归档容灾系统采购项目
- 归档,SharePoint的好伙伴
- 问答:归档产品如何保障数据安全(上)
- 2012 ISTE国际教育行业技术大会
- 保险IT应用高峰论坛
- 参加微软全球合作伙伴大会
- MessageSolution参加第五届中国保险IT应用高峰论坛(保网)
- SharePoint 2010 文档管理(三)过期归档工具
- ORACLE 9I&10G置归档方法
- Linux下用Archivemount把tar归档文件映射为虚拟磁盘
- Obect-C:Archiving归档小结
- objective C归档
- 非归档模式下数据文件移动方法
- 非归档模式下数据文件移动方法
- log_archive_dest_n设置为alternate时的讨论
- 将oracle数据库由未归档模式更改为归档模式
- OC笔记 - 数据存取(2015.3.19)
- Object-C学习(十):归档
- 归档撑爆之后不能开机的问题
- 归档