码迷,mamicode.com
首页 > 移动开发 > 详细

归档-ios

时间:2014-08-16 12:32:10      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:os   io   for   ar   log   new   ad   on   

/****归档**/

        NSString *homePath=NSHomeDirectory();

        NSString *fileName=@"test.vse";

        NSString *path=[homePath stringByAppendingPathComponent:fileName];

        NSArray *array=@[@"abc",@"sea",@123];

        BOOL success=[NSKeyedArchiver archiveRootObject:array toFile:path];

        if (success) {

            NSLog(@"success");

        }

        

        /**解存档***/

        NSArray *newArray=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

        NSLog(@"%@",newArray);

        

        /***第二种存档方式***/

        NSString *homePath2=NSHomeDirectory();

        NSString *fileName2=@"test2.vse";

        NSString *path2=[homePath2 stringByAppendingPathComponent:fileName2];

        NSMutableData *data=[NSMutableData data];

        NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

        NSArray *array2=@[@"abc222",@"se22a",@1233];

        [archiver encodeInt:100 forKey:@"age"];

        [archiver encodeObject:array2 forKey:@"name"];

        [archiver finishEncoding];

        BOOL success2=[data writeToFile:path2  atomically:YES];

        if (success2) {

            NSLog(@"success");

        }

        

        /***解第二种存档方式***/

        NSData *data2=[NSData dataWithContentsOfFile:path2];

        NSKeyedUnarchiver *unAchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data2];

        int age=[unAchiver decodeIntForKey:@"age"];

        NSArray *name=[unAchiver decodeObjectForKey:@"name"];

        NSLog(@"age=%d,name=%@",age,name);

归档-ios,布布扣,bubuko.com

归档-ios

标签:os   io   for   ar   log   new   ad   on   

原文地址:http://www.cnblogs.com/clarence/p/3916273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!