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对象
时间: 2024-10-12 13:14:52