NSKeyedArchiver和NSKeydUnarchiver

NSKeyedArchiver和NSKeydUnarchiver

1.参考
    参考自:
http://blog.csdn.net/xiaominghimi/article/details/6948046
2.NSKeyedArchiver
 1 -(void) testNSKeyedArchiver
 2 {
 3     // define game data 1
 4     NSString *stringGameData1 = @"游戏数据1";
 5     // define game data 2
 6     NSString *stringGameData2 = @"游戏数据2";
 7     // get game data
 8     NSArray *arrayGameData = [NSArray arrayWithObjects: stringGameData1, stringGameData2, nil];
 9     // get directory
10     NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
11     // get file name
12     NSString *stringFileName = [stringPath stringByAppendingPathComponent: @"FileTestNSKeyedArchiver"];
13     // failed to archive
14     if(![NSKeyedArchiver archiveRootObject: arrayGameData toFile: stringFileName]) {
15         CCLOG(@"testNSKeyedArchiver error");
16     }
17     // archive successfully
18     else {
19         CCLOG(@"%@", stringFileName);
20     }
21 }
3.NSKeyedUnarchiver
 1 -(void) testNSKeyedUnarchiver
 2 {
 3     // get directory
 4     NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
 5     // get file name
 6     NSString *stringFileName = [stringPath stringByAppendingPathComponent: @"FileTestNSKeyedArchiver"];
 7     // get game data
 8     NSArray *arrayGameData = [NSKeyedUnarchiver unarchiveObjectWithFile: stringFileName];
 9     // get game data 1
10     NSString *stringGameData1 = [arrayGameData objectAtIndex: 0];
11     // get game data 2
12     NSString *stringGameData2 = [arrayGameData objectAtIndex: 1];
13     // output game data
14     CCLOG(@"%@ %@", stringGameData1, stringGameData2);
15 }

posted on 2013-02-03 14:59  紫澜  阅读(1699)  评论(0编辑  收藏  举报