标签:
一、NSBundle资源包。
只要把文件拖到Xcode左边项目导航面板中,选择复制文件到项目中,该文件就包含进bundle中了。用[NSBundle mainBundle]获取应用程序包,常用的方法:
二、NSKeyedArchiver 、NSKeyedUnarchiver归档和恢复。
归档就是用某种格式把一个或多个对象保存到指定的文件中(把对象转化为可保存、可传输的数据流),在需要的时候再从文件中恢复它们(从数据流中恢复该对象)。
NSKeyedArchiver和NSKeyedUnarchiver继承自NSCoder。
1、NSKeyedArchiver的简单用法(适用于非自定义OC对象)。
1 NSDictionary *dict = @{@"xiaohong" : @"小红", @"xiaoming" : @"小明"};
2 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
3 path = [path stringByAppendingPathComponent:@"dict.arch"];
4 BOOL isSucc = [NSKeyedArchiver archiveRootObject:dict toFile:path];
1 NSDictionary *dict = @{@"xiaohong" : @"小红", @"xiaoming" : @"小明"};
2 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict];
3 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
4 path = [path stringByAppendingPathComponent:@"data.arch"];
5 [data writeToFile:path atomically:YES];
1 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
2 path = [path stringByAppendingPathComponent:@"dict.arch"];
3 NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
1 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
2 path = [path stringByAppendingPathComponent:@"data1.arch"];
3 NSData *data = [NSData dataWithContentsOfFile:path];
4 NSDictionary *dicts = [NSKeyedUnarchiver unarchiveObjectWithData:data];
2、NSCoding协议的用法(适用于自定义OC对象)。
如果程序需要归档或恢复任意自定义对象,则该类必需实现NSCoding协议,并实现协议中定义的方法。
1 #import <UIKit/UIKit.h>
2
3 @interface HLPerson : NSObject <NSCoding>
4
5 @property (copy, nonatomic) NSString *name;
6 @property (assign, nonatomic) CGFloat height;
7
8 @end
1 #import "HLPerson.h"
2
3 @implementation HLPerson
4
5 - (void)encodeWithCoder:(NSCoder *)aCoder {
6 [aCoder encodeObject:self.name forKey:@"name"];
7 [aCoder encodeFloat:self.height forKey:@"height"];
8 }
9
10 - (instancetype)initWithCoder:(NSCoder *)aDecoder {
11 self.name = [aDecoder decodeObjectForKey:@"name"];
12 self.height = [aDecoder decodeFloatForKey:@"height"];
13 return self;
14 }
15
16 @end
然后就可以像归档普通对象一样归档我们自定义的HLPerson对象了,参看1。
3、用NSData自定义归档。
前面的方法只能归档或恢复单个的OC对象,若要归档或恢复多个对象到单个文件中,则可以借助NSMutableData来创建NSKeyedArchiver或者NsKeyedUnarchiver对象。
(1)、归档步骤。
1 HLPerson *person = [[HLPerson alloc] init]; // 自定义的对象需实现NSCoding协议
2 person.name = @"hahh";
3 person.height = 55;
4
5 NSDictionary *dict = @{@"xiaohong" : @"小红", @"xiaoming" : @"小明"};
6 NSArray *array = @[person, dict];
7 CGFloat age = 12.5;
8
9 NSMutableData *data = [NSMutableData data];
10 NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
11 [arch encodeObject:person forKey:@"person"];
12 [arch encodeObject:dict forKey:@"dict"];
13 [arch encodeObject:array forKey:@"array"];
14 [arch encodeFloat:age forKey:@"age"];
15 [arch finishEncoding];
16
17 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
18 path = [path stringByAppendingPathComponent:@"data.arch"];
19 BOOL isSucc = [data writeToFile:path atomically:YES];
(2)、恢复步骤。
1 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
2 path = [path stringByAppendingPathComponent:@"data.arch"];
3 NSData *data = [NSData dataWithContentsOfFile:path];
4 NSKeyedUnarchiver *unarch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
5
6 HLPerson *person = [unarch decodeObjectForKey:@"person"];
7 NSDictionary *dict = [unarch decodeObjectForKey:@"dict"];
8 NSArray *array = [unarch decodeObjectForKey:@"array"];
9 CGFloat age = [unarch decodeFloatForKey:@"age"];
10
11 [unarch finishDecoding];
延伸:归档会将整个对象转换成字节数据,包括该对象的所有成员变量,如果该成员变量指向另一个OC对象,则此OC对象也将一起被归档成字节数据。这就表明:当程序归档一个对象时,系统会把该对象关联的所有数据,都转换为字节数据。如果程序从这些字节数据中恢复对象,则恢复出来的对象和原来的对象相同,但在内存上的地址不同,这就实现了对象的深复制。
标签:
原文地址:http://www.cnblogs.com/hankkk/p/5731627.html