標籤:
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 NJContatc14 15 - (void)encodeWithCoder:(NSCoder *)aCoder16 {17 [aCoder encodeObject:self.name forKey:@"name"];18 [aCoder encodeObject:self.phoneNumber forKey:@"number"];19 }20 21 - (id)initWithCoder:(NSCoder *)aDecoder22 {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