Object-C 归档、解档
2016-04-20 16:24
288 查看
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
归档(NSKeyedArchiver)/解档(NSKeyedUnarchiver)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
归档--》
NSArray *arr = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
NSMutableData * data = [NSMutableData data];
NSKeyedArchiver * arc = [NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[arc encodeWithObjects:arr forKey:@"array"];
[arc encodeWithInt:100 forKey:@"age"];
[arc encodeWithObject:@"hello world" forKey:@"name"];
[arc finishEncoding];
[arc release];
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"arc.txt"];
BOOL success = [data writeToFile:filePath atomically:YES];//success YES 归档成功
文件解档-------------》
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"arc.txt"];
NSData * data = [[ NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver * unarc =[ [NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSArray * arr = [unarc decodeObjectForKey:@"array"];
int age = [unarc decodeIntForKey:@"age"];
NSString *name = [unarc decodeObjectForKey:@"name"];
[data release];
[unarc release];
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
对象的归档、解档
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
类似java实现接口一样,OC实现NSCoding协议
重写两个方法:initWithCoder:NSCoder * 和 encodeWithCoder:NSCoder *
-(id) initWithCoder:(NSCoder *) aDecoder{
self = [super init];
if(self){
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decoderObjectForKey:@"age"];
self.number = [aDecoder decoderObjectForKey:@"number"];
}
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
[aCoder encodeObject:_number forKey:@"number"];
}
Person *per = [[Person alloc] init];
per.name = @"hello";
per.age = 12;
per.number = @"10086";
NSString *path = [NSHomeDirectory stringByAppendingPathComponent:@"persion.plist"];
BOOL success = [NSKeyedArchiver archiveRootObject:per toFilePath:path];
[per release];
//解档对象
Person * per = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
--》
解档
归档(NSKeyedArchiver)/解档(NSKeyedUnarchiver)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
归档--》
NSArray *arr = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
NSMutableData * data = [NSMutableData data];
NSKeyedArchiver * arc = [NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[arc encodeWithObjects:arr forKey:@"array"];
[arc encodeWithInt:100 forKey:@"age"];
[arc encodeWithObject:@"hello world" forKey:@"name"];
[arc finishEncoding];
[arc release];
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"arc.txt"];
BOOL success = [data writeToFile:filePath atomically:YES];//success YES 归档成功
文件解档-------------》
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"arc.txt"];
NSData * data = [[ NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver * unarc =[ [NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSArray * arr = [unarc decodeObjectForKey:@"array"];
int age = [unarc decodeIntForKey:@"age"];
NSString *name = [unarc decodeObjectForKey:@"name"];
[data release];
[unarc release];
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
对象的归档、解档
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
类似java实现接口一样,OC实现NSCoding协议
重写两个方法:initWithCoder:NSCoder * 和 encodeWithCoder:NSCoder *
-(id) initWithCoder:(NSCoder *) aDecoder{
self = [super init];
if(self){
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decoderObjectForKey:@"age"];
self.number = [aDecoder decoderObjectForKey:@"number"];
}
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
[aCoder encodeObject:_number forKey:@"number"];
}
Person *per = [[Person alloc] init];
per.name = @"hello";
per.age = 12;
per.number = @"10086";
NSString *path = [NSHomeDirectory stringByAppendingPathComponent:@"persion.plist"];
BOOL success = [NSKeyedArchiver archiveRootObject:per toFilePath:path];
[per release];
//解档对象
Person * per = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
--》
解档
相关文章推荐
- Objective-C Associated Objects 的实现原理
- Objective-C----内存管理
- Obtaining the event object
- 瀑布流
- Objective-C Runtime
- Objective-C中#define的常见用法
- javascript中Object使用详解
- Objective-c #define 和 static const
- Objective-C Runtime
- Objective-C如何把UTF-8字符串转换为十六进制数
- block没那么难(三):block和对象的内存管理
- block没那么难(二):block和变量的内存管理
- block没那么难(一):block的实现
- CEPH OBJECTSTORE API介绍
- Object-C 协议
- Objective - C 编码规范
- Objective-c 的一些学习3 Category
- iOS 开发笔记-Objective-C之KVC、KVO
- 使用visual studio 2015调用阿里云oss .net sdk 2.2的putobject接口抛出outofmemory异常
- performSelector withObject afterDelay 在子线程上调用不运行