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);

  • 相关阅读:
    鼠标事件
    表单事件
    打印20行10列的星形矩形
    打印nn乘法表
    小芳的妈妈每天给她2.5元钱,她都会存起来,但是,每当这一天是存钱的第5天或者5的倍数的话,她都会花去6元钱,请问,经过多少天,小芳才可以存到100元钱。
    我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?
    匿名函数
    冒泡排序
    session --中间件
    对cookie-parser的理解(签名、加密)
  • 原文地址:https://www.cnblogs.com/clarence/p/3916273.html
Copyright © 2011-2022 走看看