iOS对象序列化 .
来源:互联网 发布:笔记本按ubuntu黑屏 编辑:程序博客网 时间:2024/06/14 20:48
系统对象的归档我就不介绍了,这个不复杂,自己看一下就会了。
我在这里主要介绍自定义对象的归档。
Sample.h文件
- //
- // Sample.h
- // Serialization
- //
- // Created by 周 敏 on 12-11-1.
- // Copyright (c) 2012年 周 敏. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Sample : NSObject<NSCoding> {
- NSString* name;
- int magicNumber;
- float shoeSize;
- NSMutableArray *subThingies;
- }
- @property(copy) NSString* name;
- @property int magicNumber;
- @property float shoeSize;
- @property (retain) NSMutableArray *subThingies;
- -(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float) ss;
- @end
//// Sample.h// Serialization//// Created by 周 敏 on 12-11-1.// Copyright (c) 2012年 周 敏. All rights reserved.//#import <Foundation/Foundation.h>@interface Sample : NSObject<NSCoding> { NSString* name;int magicNumber;float shoeSize;NSMutableArray *subThingies;}@property(copy) NSString* name;@property int magicNumber;@property float shoeSize;@property (retain) NSMutableArray *subThingies;-(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float) ss;@end
- //
- // Sample.m
- // Serialization
- //
- // Created by 周 敏 on 12-11-1.
- // Copyright (c) 2012年 周 敏. All rights reserved.
- //
- #import "Sample.h"
- @implementation Sample
- @synthesize name;
- @synthesize magicNumber;
- @synthesize shoeSize;
- @synthesize subThingies;
- -(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float)ss
- {
- if (self=[super init])
- {
- self.name = n;
- self.magicNumber = m;
- self.shoeSize = ss;
- self.subThingies = [NSMutableArray array];
- }
- return (self);
- }
- -(void) dealloc
- {
- [name release];
- [subThingies release];
- [super dealloc];
- }
- //将对象编码(即:序列化)
- -(void) encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:name forKey:@"name"];
- [aCoder encodeInt:magicNumber forKey:@"magicNumber"];
- [aCoder encodeFloat:shoeSize forKey:@"shoeSize"];
- [aCoder encodeObject:subThingies forKey:@"subThingies"];
- }
- //将对象解码(反序列化)
- -(id) initWithCoder:(NSCoder *)aDecoder
- {
- if (self=[super init])
- {
- self.name = [aDecoder decodeObjectForKey:@"name"];
- self.magicNumber = [aDecoder decodeIntForKey:@"magicNumber"];
- self.shoeSize = [aDecoder decodeFloatForKey:@"shoeSize"];
- self.subThingies = [aDecoder decodeObjectForKey:@"subThingies"];
- }
- return (self);
- }
- -(NSString*) description
- {
- NSString *description = [NSString stringWithFormat:@"%@:%d/%.1f %@",name,magicNumber,shoeSize,subThingies];
- return (description);
- }
- @end
//// Sample.m// Serialization//// Created by 周 敏 on 12-11-1.// Copyright (c) 2012年 周 敏. All rights reserved.//#import "Sample.h"@implementation Sample@synthesize name;@synthesize magicNumber;@synthesize shoeSize;@synthesize subThingies;-(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float)ss{if (self=[super init]){self.name = n;self.magicNumber = m;self.shoeSize = ss;self.subThingies = [NSMutableArray array];}return (self);}-(void) dealloc{[name release];[subThingies release];[super dealloc];}//将对象编码(即:序列化)-(void) encodeWithCoder:(NSCoder *)aCoder{[aCoder encodeObject:name forKey:@"name"];[aCoder encodeInt:magicNumber forKey:@"magicNumber"];[aCoderencodeFloat:shoeSize forKey:@"shoeSize"];[aCoderencodeObject:subThingies forKey:@"subThingies"];}//将对象解码(反序列化)-(id) initWithCoder:(NSCoder *)aDecoder{if (self=[super init]){self.name = [aDecoder decodeObjectForKey:@"name"];self.magicNumber = [aDecoder decodeIntForKey:@"magicNumber"];self.shoeSize = [aDecoder decodeFloatForKey:@"shoeSize"];self.subThingies = [aDecoder decodeObjectForKey:@"subThingies"];}return (self);}-(NSString*) description{NSString *description = [NSString stringWithFormat:@"%@:%d/%.1f %@",name,magicNumber,shoeSize,subThingies];return (description);}@end
使用模版
- NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];
- Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];
- [s1.subThingies addObject:@"1"];
- [s1.subThingies addObject:@"2"];
- //序列化
- NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:s1];//将s1序列化后,保存到NSData中
- [s1 release];
- [data1 writeToFile:path atomically:YES];//持久化保存成物理文件
- //反序列化
- NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件
- Sample *s2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化
- NSLog(@"%@",s2);
NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5]; [s1.subThingies addObject:@"1"]; [s1.subThingies addObject:@"2"]; //序列化 NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:s1];//将s1序列化后,保存到NSData中 [s1 release]; [data1 writeToFile:path atomically:YES];//持久化保存成物理文件 //反序列化 NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件 Sample *s2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化 NSLog(@"%@",s2);
如果是多个这类对象组成的数组,序列化也很简单,只须对这个数组进行序列化。
- Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];
- [s1.subThingies addObject:@"1"];
- [s1.subThingies addObject:@"2"];
- Sample *s2 = [[Sample alloc] initWithName:@"thing2" magicNumber:22 shoeSize:22.2];
- [s2.subThingies addObject:@"22"];
- [s2.subThingies addObject:@"22"];
- NSArray *array = [NSArray arrayWithObjects:s1, s2, nil];
- [s1 release];
- [s2 release];
- NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];
- //序列化
- NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:array];
- [data1 writeToFile:path atomically:YES];//持久化保存成物理文件
- //房序列化
- NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件
- NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化
- NSLog(@"%@",array2);
Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5]; [s1.subThingies addObject:@"1"]; [s1.subThingies addObject:@"2"]; Sample *s2 = [[Sample alloc] initWithName:@"thing2" magicNumber:22 shoeSize:22.2]; [s2.subThingies addObject:@"22"]; [s2.subThingies addObject:@"22"]; NSArray *array = [NSArray arrayWithObjects:s1, s2, nil]; [s1 release];[s2 release];NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];//序列化 NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:array];[data1 writeToFile:path atomically:YES];//持久化保存成物理文件 //房序列化 NSData *data2 = [NSData dataWithContentsOfFile:path];//读取文件 NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//反序列化 NSLog(@"%@",array2);
原处:http://www.cnblogs.com/yjmyzz/archive/2011/03/03/1969859.html
- iOS对象序列化 .
- iOS对象序列化
- IOS 对象序列化
- IOS 之对象序列化
- iOS对象的序列化
- iOS数据存取和对象序列化
- iOS文档序列化(对象归档)
- iOS数据存取和对象序列化
- IOS 对象序列化(NSCoder)
- iOS NSKeyedArchiver序列化 存储数组对象
- IOS 对象序列化与反序列化NSKeyedUnarchiver
- iOS对象序列化和反序列化
- iOS 对象序列化与反序列化
- NScoder / NScoding (iOS对象序列化与反序列化)
- swift IOS中对象序列化与反序列化
- iOS中复杂实体类对象的json序列化
- ios中复杂实体类对象的json序列化
- Java对象序列化
- 实时storm程序架构图
- UVA 11137 Ingenuous Cubrency
- ibm competency
- linux设备驱动归纳总结(十一):写个简单的看门狗驱动
- select、poll、epoll的比较
- iOS对象序列化 .
- Ubuntu_搭建Lua安装环境
- tomcat-quartz appears to have started a thread named [startQuertz_Worker-1] but has failed to stop i
- 【Android 工具】TraceView使用方法
- 人生无根蒂,飘如陌上尘.
- 解决【FastCGI 进程超过了配置的活动超时时限】
- 人生无根蒂,飘如陌上尘.
- 【Android 工具】Monkey工具 (自动化压力测试)
- ios 使用已经知道到的日期,获取时间