zoukankan      html  css  js  c++  java
  • 归档-ios

      /****普通对象归档**/

            NSString *homePath=NSHomeDirectory();

            NSString *fileName=@"test.vse";

            NSString *path=[homePath stringByAppendingPathComponent:fileName];

            NSArray *array=@[@"abc",@"sea",@123];

            BOOL success=[NSKeyedArchiver archiveRootObject:array toFile:path];

            if (success) {

                NSLog(@"success");

            }

            

            /**普通对象解归档***/

            NSArray *newArray=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

            NSLog(@"%@",newArray);

            

            /***第二种普通对象归档方式***/

            NSString *homePath2=NSHomeDirectory();

            NSString *fileName2=@"test2.vse";

            NSString *path2=[homePath2 stringByAppendingPathComponent:fileName2];

            NSMutableData *data=[NSMutableData data];

            NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

            NSArray *array2=@[@"abc222",@"se22a",@1233];

            [archiver encodeInt:100 forKey:@"age"];

            [archiver encodeObject:array2 forKey:@"name"];

            [archiver finishEncoding];

            BOOL success2=[data writeToFile:path2  atomically:YES];

            if (success2) {

                NSLog(@"success");

            }

            

            /***第二种普通对象解归档方式***/

            NSData *data2=[NSData dataWithContentsOfFile:path2];

            NSKeyedUnarchiver *unAchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data2];

            int age=[unAchiver decodeIntForKey:@"age"];

            NSArray *name=[unAchiver decodeObjectForKey:@"name"];

            NSLog(@"age=%d,name=%@",age,name);

     //自定义对象归档

    #import <Foundation/Foundation.h>

     @interface User : NSObject <NSCoding>

    @property (nonatomic,copy) NSString *name;

    @property (nonatomic,copy) NSString *email;

    @property (nonatomic,copy) NSString *password;

    @property (nonatomic,assign) int age;

     @end

    ------------------------------

    #import "User.h"

    #define AGE @"age"

    #define NAME @"name"

    #define EMAIL @"email"

    #define PASSWORD @"password"

    @implementation User:NSObject

    //对属性编码,归档时调用

    - (void)encodeWithCoder:(NSCoder *)aCoder{

        [aCoder encodeInt:_age forKey:AGE];

        [aCoder encodeObject:_name  forKey:NAME];

        [aCoder encodeObject:_email forKey:EMAIL];

        [aCoder encodeObject:_password forKey:PASSWORD];

    }

    //对属性解码,解归档时调用

    - (id)initWithCoder:(NSCoder *)aDecoder{

        self=[super init];

        if (self!=nil) {

          _age=[aDecoder decodeIntForKey:AGE];

          _name=[aDecoder decodeObjectForKey:NAME];

          _email=[aDecoder decodeObjectForKey:EMAIL];

          _password=[aDecoder decodeObjectForKey:PASSWORD];

        }

        return self;

    }

    //重写description

    -(NSString *) description{

        NSString *str=[NSString stringWithFormat:@"age=%d,name=%@,email=%@,password=%@",_age,_name,_email,_password];

        return str;

    }

     @end

    ----------------使用---------------------

         /***第三种自定义对象归档方式***/

            User *user=[[User alloc] init];

            user.name=@"caicai";

            user.age=22;

            user.email=@"adf@s.com";

            user.password=@"skjdsfsd";

            

            NSString *path3=[NSHomeDirectory() stringByAppendingPathComponent:@"user.hs"];

            BOOL success3=[NSKeyedArchiver archiveRootObject:user toFile:path3];

            if (success3) {

                NSLog(@"success");

            }

             /***自定义对象解归档方式***/

            User *user2=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

            NSLog(@"%@",user2);

  • 相关阅读:
    jsp页面a标签URL转码问题
    函数的真面目实例
    野指针和内存操作实例
    redhat安装VMware tools的方法
    线索化二叉树实例
    遍历二叉树实例
    创建二叉树实例
    树的存储结构实例
    树的定义实例
    HBase基础和伪分布式安装配置
  • 原文地址:https://www.cnblogs.com/clarence/p/3916273.html
Copyright © 2011-2022 走看看