zoukankan      html  css  js  c++  java
  • 自定义类的归档解挡操作

    1.我们自己创建一个Student类,它里面有三个属性,并遵循了<NSCoding>协议

    #import <Foundation/Foundation.h>

     @interface Student : NSObject<NSCoding>

    @property (nonatomic ,strong) NSString *name;

    @property (nonatomic ,assign)  int age;

    @property (nonatomic ,strong) NSString *adder;

    @end

    #import "Student.h"

    @implementation Student

    -(NSString *)description{

        return [NSString stringWithFormat:@"name is: %@, age is: %i,addr is: %@",self.name,self.age,self.adder];

    }

    //+ (BOOL)supportsSecureCoding{

    //    return YES;

    //}

    //实现解挡方法

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

        if (self = [super init]){

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

          self.age =  [aDecoder decodeIntForKey:@"age"];

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

        }

        return self;

    }

    //实现归档方法

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

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

        [aCoder  encodeInteger:self.age forKey:@"age"];

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

    }

    @end

    在ViewController.h定义两个按钮来触发归档解挡

    #import <UIKit/UIKit.h>

    #import "Student.h"

    @interface ViewController : UIViewController

    @property (nonatomic ,strong) UIButton *btn;

    @property (nonatomic ,strong) UIButton *btn1;

    @end

    #import "ViewController.h"

    #define PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"student.liu"]

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];

        [self.btn setTitle:@"DATA归档" forState:UIControlStateNormal];

        [self.btn addTarget:self action:@selector(dataStudenst) forControlEvents:UIControlEventTouchUpInside];

        self.btn.backgroundColor = [UIColor redColor];

        [self.view addSubview:self.btn];

        self.btn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 150, 200, 40)];

        [self.btn1 setTitle:@"DATA解归档" forState:UIControlStateNormal];

        [self.btn1 addTarget:self action:@selector(jiedata) forControlEvents:UIControlEventTouchUpInside];

        self.btn1.backgroundColor = [UIColor redColor];

        [self.view addSubview:self.btn1];

        NSLog(@"%@",PATH);

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    //归档

    -(void)dataStudenst{

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

           stu.name = @"liumu";

           stu.age = 22;

           stu.adder = @"guiyangxueyuan";

      //  NSMutableData *data = [NSMutableData data];

      //  NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

     BOOL bol = [NSKeyedArchiver archiveRootObject:stu toFile:PATH];

        

    //    [archive encodeObject:stu forKey:@"stu"];

    //    [archive finishEncoding];

    //   // NSLog(@"%@",data);

    //    

    //  BOOL bol = [data writeToFile:PATH atomically:YES];

        NSLog(@"%i",bol);

        

    }

    //解挡

    -(void)jiedata{

    //    NSData *data = [NSData dataWithContentsOfFile:PATH];

    //    NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

    //      Student *stu = [unarchive decodeObjectForKey:@"stu"];

        Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:PATH];

        NSLog(@"%@",stu);

    }

    @end

  • 相关阅读:
    Windows Server 2008关闭internet explorer增强的安全配置
    【转载并整理】mysql分页方法
    Mysql:MyIsam和InnoDB的区别
    【转载】web网站css,js更新后客户浏览器缓存问题,需要刷新才能正常展示的解决办法
    【转载】java前后端 动静分离,JavaWeb项目为什么我们要放弃jsp?
    Redis命令汇总
    Redis介绍及安装
    【转载】Spring Cache介绍
    简单示例:Spring4 整合 单个Redis服务
    【转载整理】Hibernater的锁机制
  • 原文地址:https://www.cnblogs.com/liumu/p/5303987.html
Copyright © 2011-2022 走看看