【非凡程序员】 OC第十七节课 文件操作二 (归档和解档)


        //-----------------------------归档和解档-----(重点)-------、-----------//
        //可变的文件流
        NSMutableData *nutabdata=[[NSMutableData alloc]init];
        //把用归档格式的数据值给可变的文件流

        NSKeyedArchiver *keyde=[[NSKeyedArchiver alloc]initForWritingWithMutableData:nutabdata];
        //把集合形式以归档对形式编码

        [keyde encodeObject:dict forKey:@"suisui"];
        //完成归档

        [keyde finishEncoding];
       
       
//把归档过来的可变大小的数据流 写入文件中,永久存储
        [nutabdata writeToFile:@"/Users/feifanchengxuyuan/Desktop/Friday3.plist" atomically:YES];
       
      
  // 实例化一个解档对象  注意:初始化为解档格式并要放入药解档的数据流
        NSKeyedUnarchiver *unkeyed=[[NSKeyedUnarchiver alloc]initForReadingWithData:nutabdata];
        NSArray *nasrr=[unkeyed decodeObjectForKey:@"suisui"];
        NSLog(@"%@",nasrr)
;
        [nasrr writeToFile:@"/Users/feifanchengxuyuan/Desktop/Friday3.plist"  atomically:YES];
        


        //第二次打开时--解档
        [[unkeyed decodeObjectForKey:@"suisui"]writeToFile:@"/Users/feifanchengxuyuan/Desktop/Friday3.plist" atomically:YES];
        NSString *yy=[NSString stringWithContentsOfFile:@"/Users/feifanchengxuyuan/Desktop/Friday3.plist" encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"----%@",yy);

    
        
    }

你可能感兴趣的:(非凡程序员)