【Demo 0027】FoundationKit 存取自定义对象
一、自定义对象的存取方法
首先自定义类型需要继承NSCoding协议,并实现以下两个方法(消息)
-(id) initWithCoder:(NSCoder *)aDecoder;
-(void) encodeWithCoder:(NSCoder *)aCoder;
二、通常类定义及实现代码
@interface className : NSObject<NSCoding>
@end
三、练习代码
// AddressCard
//-----------------------------------------------------------
@interface AddressCard : NSObject<NSCoding>
@property(nonatomic, copy) NSString* name;
@property(nonatomic, copy) NSString* phone;
@property(nonatomic, copy) NSString* email;
-(id) init: (NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email;
-(id) assignCard: (NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email;
-(id) initWithCoder:(NSCoder *)aDecoder;
-(void) encodeWithCoder:(NSCoder *)aCoder;
@end
@implementation AddressCard
@synthesize name;
@synthesize phone;
@synthesize email;
-(id) init: (NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email
{
if (self = [super init])
{
self = [self assignCard:_name phoneNumber:_phone eMail:_email];
}
returnself;
}
-(id) assignCard: (NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email;
{
[self setName:_name];
[self setPhone:_phone];
[self setEmail:_email];
returnself;
}
-(void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:nameforKey:@"name"];
[aCoder encodeObject:phoneforKey:@"phone"];
[aCoder encodeObject:emailforKey:@"email"];
}
-(id) initWithCoder:(NSCoder *)aDecoder
{
name = [aDecoder decodeObjectForKey:@"name"];
phone = [aDecoder decodeObjectForKey:@"phone"];
email = [aDecoder decodeObjectForKey:@"email"];
returnself;
}
@end
// AddressBook
//-----------------------------------------------------------
@interface AddressBook : NSObject<NSCoding>
@property(nonatomic, copy) NSMutableDictionary* addressCardList;
-(id) init;
-(id) initWithAddress:(NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email;
-(void) setAddress:(NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email;
-(void) setAddressCard: (AddressCard*) card;
-(id) initWithCoder:(NSCoder *)aDecoder;
-(void) encodeWithCoder:(NSCoder *)aCoder;
@end
@implementation AddressBook
@synthesize addressCardList;
-(id) init
{
if (self = [super init])
{
addressCardList = [NSMutableDictionarydictionaryWithCapacity:10];
}
returnself;
}
-(id) initWithAddress:(NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email;
{
if ([self init])
{
[self setAddress:_name phoneNumber:_phone eMail:_email];
}
returnself;
}
-(void) setAddress: (NSString*)_name phoneNumber: (NSString*)_phone eMail: (NSString*)_email
{
AddressCard* card = [[AddressCard alloc]init:_name phoneNumber:_phone eMail:_email];
[addressCardList setObject:card forKey:_name];
}
-(void) setAddressCard: (AddressCard*) card
{
[addressCardList setObject:card forKey:[card name]];
}
-(id) initWithCoder:(NSCoder *)aDecoder
{
if ([self init])
{
addressCardList = [aDecoder decodeObjectForKey:@"addressBook"];
}
returnself;
}
-(void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:addressCardListforKey:@"addressBook"];
}
-(void) print
{
int index = 1;
NSMutableString* description = [NSMutableStringstringWithFormat:@"\nAddressBook Contain card count: %lu\n", [addressCardListcount]];
[description appendFormat:@"%-10s %-20s %-20s %-20s\n", "id", "name", "phone", "email"];
[description appendString:@"----------------------------------------------------------------------------\n"];
for (NSString* key in addressCardList) {
AddressCard* card = [addressCardListobjectForKey:key];
[description appendFormat:@"%-10u %-20s %-20s %-20s\n", index++, [[card name] UTF8String], [[card phone] UTF8String], [[card email] UTF8String]];
}
[description appendString:@"----------------------------------------------------------------------------\n"];
NSLog(@"%@", description);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
{
AddressBook* book = [[AddressBook alloc]init];
AddressCard* card1 = [[AddressCardalloc]init:@"hanMeiMei"phoneNumber:@"132-1938-1284"eMail:@"haMeiMei@icloud.com"];
AddressCard* card2 = [[AddressCardalloc]init:@"MaLiLi"phoneNumber:@"135-0238-1134"eMail:@"MaLiLi@icloud.com"];
AddressCard* card3 = [[AddressCardalloc]init:@"LiLei"phoneNumber:@"159-1132-1235"eMail:@"LiLei@icloud.com"];
[book setAddressCard:card1];
[book setAddressCard:card2];
[book setAddressCard:card3];
[book setAddress:@"ztercel"phoneNumber:@"186-8878-6625"eMail:@"ztercel@icloud.com"];
[book print];
if ([NSKeyedArchiver archiveRootObject:book toFile:@"addressBook.data"])
{
NSLog(@"saved addressbook to file using NSKeyedArchiver");
}
NSMutableData* dataArea = [NSMutableData data];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:dataArea];
[archiver encodeObject:book forKey:@"addressBook"];
[archiver finishEncoding];
if ([dataArea writeToFile:@"addressbook.package" atomically:YES])
{
NSLog(@"saved addressbook to file using data buffer");
};
}
{
AddressBook* book = [NSKeyedUnarchiver unarchiveObjectWithFile:@"addressBook.data"];
[book print];
NSMutableData* data = [NSMutableData dataWithContentsOfFile:@"addressbook.package"];
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
AddressBook* book1 = [unarchiver decodeObjectForKey:@"addressBook"];
[unarchiver finishDecoding];
[book1 print];
}
}
return 0;
}
演示实例
浙公网安备 33010602011771号