码迷,mamicode.com
首页 > 移动开发 > 详细

ios数据存储之二 —— NSKeyedArchiver

时间:2016-01-22 03:00:22      阅读:173      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!