ObjcetiveC 归档 迎客

对于基本Objective-C类对象(NSString,NSArray...):

方法一:使用XML属性列表进行归档。

代码
1 NSDictionary *glossay;
2 //
3   glossay = [NSDictionary dictionaryWithObjectsAndKeys:@"obj val 1",@"key1",@"obj val 2",@"key2",nil];
4 if ([glossay writeToFile:@"glossary" atomically:YES] == NO) {
5 NSLog(@"Save to file failed!");
6 }
7
8 //
9   glossay = [NSDictionary dictionaryWithContentsOfFile:@"glossary"];
10
11 NSLog(@"%@",[glossay valueForKey:@"key2"]);

方法二:使用NSKeyedArchiver归档。

代码
1 NSDictionary *glossay;
2 glossay = [NSDictionary dictionaryWithObjectsAndKeys:@"obj val 1",@"key1",@"obj val 2",@"key2",nil];
3 //
4 if ([NSKeyedArchiver archiveRootObject:glossay toFile:@"glossay.archiver"] == NO) {
5 NSLog(@"write file fail!!");
6 }
7
8 //
9 glossay = [NSKeyedUnarchiver unarchiveObjectWithFile:@"glossay.archiver"];
10 NSLog(@"%@",[glossay valueForKey:@"key2"]);


对于自定义的Class,需要实现NSCoding协议,然后用上述方法二归档:

代码
1 //TestProperty.h
2  #import <Cocoa/Cocoa.h>
3
4
5 @interface TestProperty : NSObject <NSCopying,NSCoding>{
6 NSString *name;
7 NSString *password;
8 NSMutableString *interest;
9 NSInteger myInt;
10 }
11
12 @property (retain,nonatomic) NSString *name,*password;
13 @property (retain,nonatomic) NSMutableString *interest;
14 @property NSInteger myInt;
15
16  -(void) rename:(NSString *)newname;
17
18 @end
19
20 ====================
21 //TestProperty.m
22
23 #import "TestProperty.h"
24
25
26 @implementation TestProperty
27
28 @synthesize name,password,interest;
29 @synthesize myInt;
30
31 -(void) rename:(NSString *)newname{
32 // 这里可以直接写成
33 // self.name = newname;
34 //
35 if (name != newname) {
36 [name autorelease];
37 name = newname;
38 [name retain];
39 }
40 }
41
42 -(void) dealloc{
43 self.name = nil;
44 self.password = nil;
45 self.interest = nil;
46 [super dealloc];
47 }
48 - (id)copyWithZone:(NSZone *)zone{
49 TestProperty *newObj = [[[self class] allocWithZone:zone] init];
50 newObj.name = name;
51 newObj.password = password;
52 newObj.myInt = myInt;
53 //深复制
54 NSMutableString *tmpStr = [interest mutableCopy];
55 newObj.interest = tmpStr;
56 [tmpStr release];
57
58 //浅复制
59 //newObj.interest = interest;
60 return newObj;
61 }
62
63 - (void)encodeWithCoder:(NSCoder *)aCoder{
64 //如果是子类,应该加上:
65 //[super encodeWithCoder:aCoder];
66
67 //注意这里如何处理对象的(其实是实现了NSCoding的类)!
68 [aCoder encodeObject:name forKey: @"TestPropertyName"];
69 [aCoder encodeObject:password forKey:@"TestPropertyPassword"];
70 [aCoder encodeObject:interest forKey:@"TestPropertyInterest"];
71
72 //注意这里如何处理基本类型!
73 [aCoder encodeInt:myInt forKey:@"TestPropertyMyInt"];
74
75 }
76
77 - (id)initWithCoder:(NSCoder *)aDecoder{
78 //如果是子类,应该加上:
79 //self = [super initWithCoder:aDecoder];
80
81 //解码对象
82 name = [[aDecoder decodeObjectForKey:@"TestPropertyName"] retain];
83 password = [[aDecoder decodeObjectForKey:@"TestPropertyPassword"] retain];
84 interest = [[aDecoder decodeObjectForKey:@"TestPropertyInterest"] retain];
85
86 //解码基本类型
87 myInt = [aDecoder decodeIntForKey:@"TestPropertyMyInt"];
88 return self;
89 }
90 @end
91
92 ===============
93 //测试
94
95 //
96 TestProperty *test = [[TestProperty alloc] init];
97 test.name = @"pxl";
98 test.password = @"pwd...";
99 test.interest = [NSMutableString stringWithString:@"interest..."];
100 test.myInt = 123;
101 if([NSKeyedArchiver archiveRootObject:test toFile:@"testproerty.archive"] == NO){
102 NSLog(@"write to file fail!!");
103 }
104
105 //
106 TestProperty *test = [NSKeyedUnarchiver unarchiveObjectWithFile:@"testproerty.archive"];
107 NSLog(@"%@",test.name);


使用NSData创建定义档案。

以上面已实现NSCoding协议的TestProperty类为例。

代码
1 //
2   TestProperty *test = [[TestProperty alloc] init];
3 test.name = @"pxl";
4 test.password = @"pwd...";
5 test.interest = [NSMutableString stringWithString:@"interest..."];
6 test.myInt = 123;
7
8 NSMutableData *dataArea = [NSMutableData data];
9 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataArea];
10 [archiver encodeObject:test forKey:@"testObj"];
11
12 //这里还可以加其它的对象
13 //......
14  
15 [archiver finishEncoding];
16
17 if ([dataArea writeToFile:@"test.archiver" atomically:YES] == NO) {
18 NSLog(@"write to file fail...");
19 }
20
21 [archiver release];
22 [test release];
23
24  ============
25
26  //
27   NSData *dataArea = [NSData dataWithContentsOfFile:@"test.archiver"];
28 if(!dataArea){
29 NSLog(@"Can't read back archive file");
30 return (1);
31 }
32
33 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataArea];
34 TestProperty *test = [unarchiver decodeObjectForKey:@"testObj"];
35 [unarchiver finishDecoding];
36
37 NSLog(@"%@",test.name);
38 [unarchiver release];

利用归档实现对象深复制:

代码
1 //先删除TestProperty类中实现的NSCopying协议代码。
2  
3 TestProperty *test = [[TestProperty alloc] init];
4 test.name = @"pxl";
5 test.password = @"pwd...";
6 test.interest = [NSMutableString stringWithString:@"interest..."];
7 test.myInt = 123;
8
9 //对test进行深复制
10   NSData *data = [NSKeyedArchiver archivedDataWithRootObject:test];
11 TestProperty *test2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
12
13 [test2.interest appendString:@"film"];
14 NSLog(@"%@",test.interest);
15 NSLog(@"%@",test2.interest);
16
17 //输出
18   2010-12-30 16:11:47.391 HelloWorld[4599:a0f] interest...
19 2010-12-30 16:11:47.393 HelloWorld[4599:a0f] interest...film
原文地址:https://www.cnblogs.com/pengxl/p/1921729.html