zoukankan      html  css  js  c++  java
  • Object-C-系统类型对象归档

    系统类型主要是指NSString NSDictionary,NSArray,NSData,NSNumber 类型数据(包括对应可变类型);

    这些类型已经实现了NSCoding协议,支持归档,

    写入方法:

    writeToFile:atomically:

    读取方法:

    -dictionaryWithContentsOfFile:

    -arrayWithContentsOFFile:

    -dataWithContentsOfFile:

    -stringWithContentsOfFile:

    //字符串对象归档以及解归档 atomically 缓冲池
        NSString *str=@"hello oc!";
        
        
         //写入
        BOOL falg=[str writeToFile:@"str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        if(falg){
            NSLog(@"str 保存成功!");
        }
        
        //读取
        NSString *readStr=[NSString stringWithContentsOfFile:@"str.txt" encoding:NSUTF8StringEncoding error:nil];
        
        //数组对象归档以及解归档
        //写入
        NSArray *arr=@[@"one",@"two",@"three"];
        BOOL flag=[arr writeToFile:@"arr.plist" atomically:YES];
        if(flag){
            NSLog(@"YES");
        }

       //读出
        NSArray *readArr =[NSArray arrayWithContentsOfFile:@"arr.plist"];
       
        //字典对象归档以及解归档
        NSDictionary *dic=@{@"1":@"one",
                            @"2":@"two",
                            @"3":@"three"};
        //写入
        BOOL flag = [dic writeToFile:@"dic.plist" atomically:YES];
        if(flag){
            NSLog(@"YES");
        }
         
        NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:@"dic.plist"];
        NSLog(@"readDic = %@",readDic);

  • 相关阅读:
    软件杯-题目和插件
    基于《河北省重大技术需求征集系统》的可用性和可修改性战术分析
    基于淘宝网的系统质量属性六大场景
    架构漫谈读后感
    06掌握需求过程阅读笔记之一
    大道至简读后感以及JAVA伪代码
    K8S学习笔记
    事务的七种传播类型、及案例
    香港身份证规则
    oracle函数
  • 原文地址:https://www.cnblogs.com/Opaser/p/4563931.html
Copyright © 2011-2022 走看看