本文共 4077 字,大约阅读时间需要 13 分钟。
#import@interface Person : NSObject @property(copy,nonatomic)NSString *name; @property(assign,nonatomic) int age; @end
#import "Person.h" @implementation Person -(id)copyWithZone:(NSZone *)zone { Person *p = [[Person alloc] init]; //拷贝函数不需要release,这里用autorelease会报错 p.name = [self.name copy]; p.age = self.age; return p; } //实现NSCoding协议 -(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInt:self.age forKey:@"age"]; //这儿要根据不同的类型来写encode的类型 } //反序列化提取成员变量 -(id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { if (aDecoder == nil) { return self; } self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntForKey:@"age"]; //这儿也是,如果是int类型就写decodeIntForKey } return self; } -(NSString *)description { return [NSString stringWithFormat:@"%@,%d",self.name,self.age]; } @end
#import#import "Person.h" @interface DXWViewController : UIViewController @property(nonatomic,retain)NSArray *Array; @property(nonatomic,retain)Person *per; @end
#import "DXWViewController.h" #define kSaveKeyMarkerLines (@"person") @interface DXWViewController () @end @implementation DXWViewController -(NSString *)getPath { //用来获得Document地址 NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//注意:这里是NSDocument不是NSDocumentation,特别注意 NSLog(@"%@",arr); //在地址上增加文件 NSString *path = [arr[0] stringByAppendingPathComponent:@"Person.plist"]; NSLog(@"%@",path); return path; } //传入可变数组和地址 //序列化数据 - (BOOL)saveMarkers:(NSMutableArray *)markers toFilePath:(NSString *)filePath { BOOL f; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *vdArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [vdArchiver encodeObject:markers forKey:kSaveKeyMarkerLines]; [vdArchiver finishEncoding]; f = [data writeToFile:filePath atomically:YES]; [vdArchiver release]; [data release]; return f; } //传入地址,返回可变数组 //反序列化数据 - (NSMutableArray *)loadMarkersFromFilePath:(NSString *)filePath { NSMutableArray *markers = nil; if (filePath == nil || [filePath length] == 0 || [[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) { markers = [[[NSMutableArray alloc] init] autorelease]; } else { NSData *data = [[NSData alloc] initWithContentsOfFile:filePath]; NSKeyedUnarchiver *vdUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; markers = [vdUnarchiver decodeObjectForKey:kSaveKeyMarkerLines]; NSLog(@"++++%@",markers); [vdUnarchiver finishDecoding]; [vdUnarchiver release]; [data release]; } return markers; } - (void)viewDidLoad { [super viewDidLoad]; Person *p1 = [[Person alloc] init]; p1.name = @"dingxiaowei"; p1.age = 22; Person *p2 = [[Person alloc] init]; p2.name = @"wangning"; p2.age = 33; self.Array = [NSArray arrayWithObjects:p1,p2, nil]; //保存到文件 [self saveMarkers:self.Array toFilePath:[self getPath]]; //从文件中获取数据 NSMutableArray *arr = [self loadMarkersFromFilePath:[self getPath]]; NSLog(@"归档后提取的数据:\n%@",arr); } @end
2013-08-26 14:04:47.101 test[1442:c07]归档后提取的数据:
(
"dingxiaowei,22",
"wangning,33"
)