[数据存储之一]NSCoding协议练习

#import <Foundation/Foundation.h>

@interface TestCodingProtocol : NSObject<NSCoding>
#pragma mark- 持久化属性
@property(nonatomic,copy,readwrite) NSString* myName;
@property(nonatomic,assign,readwrite)NSInteger myIndex;
#pragma mark- 实现协议方法
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;

@end

@interface TestCodingProtocolChild : TestCodingProtocol

@property(nonatomic,assign,readwrite) NSInteger myIndexChild;
-(void)encodeWithCoder:(NSCoder *)aCoder;
-(id)initWithCoder:(NSCoder *)aDecoder;

@end
#import "TestCodingProtocol.h"

@implementation TestCodingProtocol

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    if (aCoder) {
        [aCoder encodeObject:self.myName forKey:@"myName"];
        //self.myIndex为基础类型,不能用encodeObject
        [aCoder encodeInteger:self.myIndex forKey:@"myIndex"];
    }
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        if (!aDecoder) {
            return self;
        }
        self.myName = [aDecoder decodeObjectForKey:@"myName"];
        self.myIndex = [aDecoder decodeIntegerForKey:@"myIndex"];
    }
    return self;
}
@end

@implementation TestCodingProtocolChild

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];
    [aCoder encodeInteger:self.myIndexChild forKey:@"myIndexChild"];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {//这里如果用init的话,父类的内容得不到初始化。
        if (!aDecoder) {
            return self;
        }
        self.myIndexChild = [aDecoder decodeIntegerForKey:@"myIndexChild"];
    }
    return self;
}
@end
NSString* path = @"/Users/liyanq/Desktop";
    NSString* fPath = [path stringByAppendingPathComponent:@"copying.plist"];
    TestCodingProtocol* t = [[TestCodingProtocol alloc] init];
    t.myIndex = 5;
    t.myName = @"hello";
    
    TestCodingProtocolChild* tChild = [[TestCodingProtocolChild alloc] init];
    tChild.myIndex = 6;
    tChild.myName = @"child";
    tChild.myIndexChild = 7;
    //增加几个基础类型。
    NSString* s = @"helloworld";
    NSNumber* n = [NSNumber numberWithInteger:5];
    NSDate*   d = [NSDate date];
    NSArray*  r = [NSArray arrayWithObjects:tChild,t,s,n,d, nil];
    [NSKeyedArchiver archiveRootObject:r toFile:fPath];<span style="color: rgb(76, 191, 87); font-size: 11px; font-family: Menlo;">//</span><span style="color: rgb(76, 191, 87); font-family: 'Heiti SC Light'; font-size: 11px;">完成了存储。没有文件会创建,有的话会清除内容。</span>
    NSArray* er = [NSKeyedUnarchiver unarchiveObjectWithFile:fPath];//完成了读取。
    [er enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[TestCodingProtocolChild class]]) {
            TestCodingProtocolChild* buf = obj;//只能声明个变量了,不会强制转换。
            NSLog(@"myIndex is %ld, myName is %@, myIndexChild is %ld", buf.myIndex, buf.myName, buf.myIndexChild);
        }
        else if ([obj isKindOfClass:[TestCodingProtocol class]])
        {
            TestCodingProtocol* buf = obj;
            NSLog(@"myIndex is %ld, myName is %@", buf.myIndex, buf.myName);
        }
        else{
            NSLog(@"index is %ld,value is %@",idx,obj);
        }
    }];

总结下吧:

             1,完成了对象的序列化与反序列化,而且对象可以继承自nsobject.

             2,归档的形式来保存数据,只能一次性归档保存以及一次性解压。所以只能针对小量数据,而且对数据操作比较笨拙,即如果想改动数据的某一小部分,还是需要解压整个数据或者归档整个数据。

            3,使用NSKeyedArchiver可以归档和恢复NSString、NSArray、NSDictionary、NSSet、NSDate、NSNumber和NSData等基本的Foundation对象

参考:http://blog.csdn.net/tianyitianyi1/article/details/7713103

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值