且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于数据的归档存入文件和读取文件

更新时间:2022-09-27 13:35:10

需求:我们都知道NSArry中如果存放的是普通的字符串类型,是很容易存入到plist文件中,也很容易从文件中读取出来,那如果NSArray中存放的是自定义的Person对象呢?该如何存入文件中去呢?

下面我来简单写一个NSArray中继承了NSCoding协议的自定义Person对象,将这个array数组存入到plist文件中,并且能够实现读取文件中的数据的Demo

实现步骤:

1.创建一个SingleView的项目命名为test,然后创建一个自定义的Person对象,具有NSString *name和int age两个属性。

Person.h:

#import <Foundation/Foundation.h> @interface Person : NSObject<NSCopying,NSCoding> @property(copy,nonatomic)NSString *name; @property(assign,nonatomic) int age; @end

Person.m:

#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

2.创建一个ViewController来实现数据的的保存

ViewController.h:

#import <UIKit/UIKit.h> #import "Person.h"   @interface DXWViewController : UIViewController @property(nonatomic,retain)NSArray *Array; @property(nonatomic,retain)Person *per; @end

ViewController.m:

#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


如何寻找创建在沙盒中的文件?

点击一下mac机上的前往功能菜单->然后按下Alt键,会跳出一个资源库的一个隐藏文件->Application Support->iPhone Simulator->6.1(看你当前运行的模拟器的版本)->Applications->对应的文件->Documents->xxx.plist

Demo源码:

http://download.csdn.net/detail/s10141303/6013361

运行结果:

2013-08-26 14:04:47.101 test[1442:c07]归档后提取的数据:

(

    "dingxiaowei,22",

    "wangning,33"

)














本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366421,如需转载请自行联系原作者