• objectc中归档的用法


    下面是归档的例子

    //
    //  main.m
    //  ArchiveDemo
    //
    //  Created by ganchaobo on 13-5-4.
    //  Copyright (c) 2013年 ganchaobo. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    void archive1(){
        //******************************************归档第一种方法
        NSArray *arr=[NSArray arrayWithObjects:@"11",@"vv",@"tt", nil];
        //归档(即序列化)archiveRootObject
        NSString *path=@"/Users/ganchaobo/Desktop/archiver.text";
        BOOL iskeyedarchiver= [NSKeyedArchiver archiveRootObject:arr toFile:path];
        if(iskeyedarchiver){
            NSLog(@"archive success ");
        }
        //*******************************************对应的解归档
        NSArray *unarchivearr =[NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSLog(@"%@",unarchivearr);
    }
    
    void archive2(){
         //******************************************归档第2种方法
        NSString *path1=@"/Users/ganchaobo/Desktop/archiver1.text";
        NSMutableData *date=[NSMutableData data];
        //初始化时要把归档的内容放在date中
        NSArray *arr=[NSArray arrayWithObjects:@"yuwen",@"math", nil];
        NSKeyedArchiver *archive= [[[NSKeyedArchiver alloc] initForWritingWithMutableData:date]autorelease];
        //下面时把内容归档到一个date里面取
        [archive  encodeFloat:1.81 forKey:@"height"];
        [archive encodeObject:@"zs" forKey:@"name"];
        [archive encodeObject:@"23" forKey:@"age"];
        [archive encodeObject:arr forKey:@"kemu"];
        [archive finishEncoding];
        
        [date writeToFile:path1 atomically:YES];//此时date数据时归档后的数据,然后写入指定的文件
        //*******************************************对应的解归档
        NSData *date1=[NSData dataWithContentsOfFile:path1];//读取内容
       NSKeyedUnarchiver *unarchive= [[[NSKeyedUnarchiver alloc] initForReadingWithData:date1] autorelease];
        NSLog(@"%f",[unarchive decodeFloatForKey:@"height"]);
        NSLog(@"%@",[unarchive decodeObjectForKey:@"name"]);
         NSLog(@"%@",[unarchive decodeObjectForKey:@"age"]);
         NSLog(@"%@",[unarchive decodeObjectForKey:@"kemu"]);
    }
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            archive2();
        }
        return 0;
    }
    
  • 相关阅读:
    Oracle客户端安装与配置
    Word VBA(批量复制Excel表格和Word表格到Word中)
    批量追加数据库(GDB,MDB,Shp)
    批量横向打印Excel
    重命名要素类
    删除GIS数据属性值空格(GDB,MDB,Shp)
    删除GIS数据库空层(GDB,MDB,Shape)
    批量裁剪GIS数据(包含GDB,MDB,Shp)
    批量裁剪GDB
    3DS文件导出MultiPatch
  • 原文地址:https://www.cnblogs.com/gcb999/p/3060339.html
Copyright © 2020-2023  润新知