zoukankan      html  css  js  c++  java
  • 归档 解档

    //系统类型的对象归档(NSString/NSArray/NSDictionary

    //1、设置归档路径,该路径需要详细到文件(不能是文件夹)

    //2、得到要归档的对象

    //3、通过NSKeyedArchiver调用archiveRootObject方法,进行归档

    //4、解档 通过NSKeyedUnarchiver调用unarchiveObjectWithFile进行解档,注意,该方法返回值类型为id

     

     

     //字符串的归档 解档

        NSString *path = NSHomeDirectory();

        NSString *documents = [path stringByAppendingPathComponent:@"Documents"];

        NSString *strpath = [documents stringByAppendingPathComponent:@"string.txt"];

        NSString *newstr = @"新闻联播";

        [NSKeyedArchiver archiveRootObject:newstr toFile:strpath];

        NSLog(@"%@",strpath);

        NSString *str = [NSKeyedUnarchiverunarchiveObjectWithFile:strpath];

        NSLog(@"---%@",str);

        

        

        //数组的归档 解档

        

        NSString *arrpath = [documents stringByAppendingPathComponent:@"arr.txt"];

        NSArray *arr = @[@"1",@"2",@"3"];

        [NSKeyedArchiverarchiveRootObject:arr toFile:arrpath];

        NSArray *arr1 = [NSKeyedUnarchiver unarchiveObjectWithFile:arrpath];

        NSLog(@"%@",arr1);

     

     

     

    ///////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////

     

    #import "ViewController.h"

    #import "Student.h"

     

    @interfaceViewController ()

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/student.txt"];

        NSLog(@"%@",path);

        Student *stu= [[Student alloc]init];

        stu.name = @"张三";

        [NSKeyedArchiverarchiveRootObject:stu toFile:path];

        Student *s1 = [NSKeyedUnarchiverunarchiveObjectWithFile:path];

        NSLog(@"%@",s1.name);

        

    }

     

     

     

     

    #import <Foundation/Foundation.h>

    //如果需要对自定义类进行归档,则该类需要遵循NSCoding协议,并且实现其协议方法

    @interface Student : NSObject<NSCoding>

     

    @property (nonatomic,copy) NSString *name;

     

     

    @end

     

     

    #import "Student.h"

    @implementation Student

    //实现encodeWithCoder协议方法,当系统对自定义类的对象进行归档时,会调用此方法,归档其属性

    -(void) encodeWithCoder:(NSCoder *)aCoder

    {

        [aCoder encodeObject:self.name forKey:@"name"];

         

    }

    //实现initWithCoder方法,当系统对自定义类的对象进行解档时,会调用此方法,可以看成是一个初始化的过程,返回一个对象,并且对其属性进行解档

    -(id) initWithCoder:(NSCoder *) aDecoder

    {

        self = [super init];

        if (self) {

            self.name = [aDecoder decodeObjectForKey:@"name"];

        }

        returnself;

    }

     

    @end

     

  • 相关阅读:
    边缘计算的爆发为安防全产业带来了怎样的变化?
    Kali卸载AWVS的方法
    C++最简打开网页的方法
    C# 打开指定文件或网址
    C# 如何获取某用户的“我的文档”的目录
    基于Debian的linux系统软件安装命令
    C#的基础知识
    MYSQL语句中的增删改查
    将博客搬至CSDN
    【易懂】斜率DP
  • 原文地址:https://www.cnblogs.com/lcl15/p/5003376.html
Copyright © 2011-2022 走看看