首页 > 移动平台 > 详细

ios数据存储之二 —— NSKeyedArchiver

时间:2016-01-22 02:58:03      阅读:175      评论:0      收藏:0      [点我收藏+]
 1 #import <Foundation/Foundation.h>
 2 
 3 @interface NJContatc : NSObject <NSCoding>
 4 
 5 @property (nonatomic, copy) NSString *name;
 6 @property (nonatomic, copy) NSString *phoneNumber;
 7 @property (nonatomic, copy) NSString *email;
 8 @end
 9 
10 
11 #import "NJContatc.h"
12 
13 @implementation NJContatc
14 
15 - (void)encodeWithCoder:(NSCoder *)aCoder
16 {
17     [aCoder encodeObject:self.name forKey:@"name"];
18     [aCoder encodeObject:self.phoneNumber forKey:@"number"];
19 }
20 
21 - (id)initWithCoder:(NSCoder *)aDecoder
22 {
23     if (self = [super init]) {
24         self.name = [aDecoder decodeObjectForKey:@"name"];
25         self.phoneNumber = [aDecoder decodeObjectForKey:@"number"];
26     }
27     return self;
28 }
29 @end

存储:

        [NSKeyedArchiver archiveRootObject:self.contatcs toFile:contactsPath]; // 此种情况存的是contacts的集合

        [NSKeyedArchiver archiveRootObject:self.contact toFile:contactsPath]; // 此种情况存的contact对象

读取:

         self.contatcs =  [NSKeyedUnarchiver unarchiveObjectWithFile:contactsPath]; // 此种情况获取的是contact的集合

        self.contact =  [NSKeyedUnarchiver unarchiveObjectWithFile:contactsPath]; // 此种情况获取的是contact对象

 

  

 

 

    

 

ios数据存储之二 —— NSKeyedArchiver

原文:http://www.cnblogs.com/PJHome/p/5150054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!