归档

    NSArray* array=@[@"ze",@"feng",@"xie"];
    NSString * path =[NSHomeDirectory() stringByAppendingPathComponent:@"array.plist"];
    BOOL isSuccess=[NSKeyedArchiver archiveRootObject:array toFile:path];
    if (isSuccess) {
        NSLog(@"归档成功%@",path);
    }

    //解档
    NSString * filepath=[NSHomeDirectory() stringByAppendingPathComponent:@"array.plist"];
    id arr =[NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
    NSLog(@"%@",arr);
    
    
    //第二种归档
    NSArray * arr1=@[@"x",@"z",@"f"];
    NSMutableData * data=[NSMutableData data];
    NSKeyedArchiver  * archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    //编码数据和对象
    [archiver encodeObject:arr1 forKey:@"arrar"];
    [archiver encodeObject:@100 forKey:@"scope"];
    
    //完成归档,将归档数据填充至data中,此时data中已经存储了归档对象的数据
    [archiver finishEncoding];
    NSString * file=[NSHomeDirectory() stringByAppendingPathComponent:@"arr.txt"];
    BOOL success=[data writeToFile:file atomically:YES];
    if (success) {
        NSLog(@"写入文件");
        
    }
    //
    NSData * data1=[[NSData alloc]initWithContentsOfFile:file];
    NSKeyedUnarchiver * u=[[NSKeyedUnarchiver alloc]initForReadingWithData:data1];
    id a=[u decodeObjectForKey:@"arrar"];
    NSLog(@"a======%@",a);

posted @ 2015-06-15 10:51  谢小锋  阅读(182)  评论(0编辑  收藏  举报