年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2177|回复: 0

第二十三讲:文件归档与协议

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-1-21 22:51:49 | 显示全部楼层 |阅读模式
    1、对象进行归档操作时需要用到协议NSCoding,前面已经有讲到存档与解档操作,这里引进协议的概念,在协议中实现归档
    2、创建一个类Person
    3、在Person中使用协议NSCoding来实现存档操作
    4、在implemetation中实现协议NSCoding的两个方法(必须实现)
    5、对象person1归档到data中,data1将解档data;
    6、创建person对象,将data1解档的数据赋值给person

    Person.h
    1. //
    2. //  Person.h
    3. //  NSCoding
    4. //
    5. //  Created by yusian on 14-1-21.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface Person : NSObject <NSCoding>
    10. @property (retain) NSString * name;
    11. @property int age;
    12. @property (retain) Person * child;
    13. @end
    复制代码
    Person.m
    1. //
    2. //  Person.m
    3. //  NSCoding
    4. //
    5. //  Created by yusian on 14-1-21.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "Person.h"
    9. @implementation Person
    10. - (void)encodeWithCoder:(NSCoder *)aCoder {
    11.     [aCoder encodeObject:_name forKey:@"name"];
    12.     [aCoder encodeInt:_age forKey:@"age"];
    13.     [aCoder encodeObject:_child forKey:@"chile"];
    14. }
    15. - (id)initWithCoder:(NSCoder *)aDecoder {
    16.     if (self = [super init]) {
    17.         self.age = [aDecoder decodeIntForKey:@"age"];
    18.         self.name = [aDecoder decodeObjectForKey:@"name"];
    19.         self.child = [aDecoder decodeObjectForKey:@"chile"];
    20.     }
    21.     return self;
    22. }
    23. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  NSCoding
    4. //
    5. //  Created by yusian on 14-1-21.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "Person.h"
    10. int main(int argc, const char * argv[])
    11. {
    12.     @autoreleasepool {
    13.         
    14.         Person * person1 = [[Person alloc] init];
    15.         Person * person2 = [[Person alloc] init];
    16.         
    17.         person1.age = 30;
    18.         person1.name = @"deny";
    19.         person1.child = person2;
    20.         
    21.         NSData * data = [NSKeyedArchiver archivedDataWithRootObject:person1];
    22.         [data writeToFile:@"/tmp/file.plist" atomically:YES];
    23.         
    24.         NSData * data1 = [NSData dataWithContentsOfFile:@"/tmp/file.plist"];
    25.         Person * person = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
    26.         
    27.         NSLog(@"Person %@ age is %d",person.name, person.age);
    28.         
    29.     }
    30.     return 0;
    31. }
    复制代码
    输出结果:
    1. 2014-01-21 22:46:20.541 NSCoding[2287:303] Person deny age is 30
    2. Program ended with exit code: 0
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-6 02:32 , Processed in 0.043133 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表