【非凡程序員】 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];
        
ui


        //第二次打開時--解檔
        [[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);

    
        
    }
編碼

相關文章
相關標籤/搜索