在IOS的开发中,小数据量的持久化都用NSUserDefaults来实现,但是NSUserDefaults只能保存NSString, NSNumber, NSDate, NSArray, NSDictionary这些数据类型,但大多时候,我们会将一个对象实体做持久化的保存,由于不是大批量的数据,不会用到sqlite,那么这个时候NSUserDefaults会是很好的选择,其实对象类型可以通过NSCoding的委托方法来实现的。
[cpp] view plaincopy
- View Code //
- // UserEntity.m
- // NSCodingDemo
- //
- // Created by on 11-5-20.
- // Copyright 2011 , Inc. All rights reserved.
- //
- #import "UserEntity.h"
- #import "AddressEntity.h"
- #define SAMPLEDATA_KEY_USERNAME (@"userName")
- #define SAMPLEDATA_KEY_GENDER (@"gender")
- #define SAMPLEDATA_KEY_AGE (@"age")
- #define k_FRIENDS (@"frinds")
- #define k_ADDRESS (@"address")
- #define k_userEntity_addresses (@"addresses")
- @implementation UserEntity
- @synthesize userName = _userName;
- @synthesize gender = _gender;
- @synthesize age = _age;
- @synthesize friends = _friends;
- @synthesize address = _address;
- @synthesize addresses = _addresses;
- -(void)dealloc
- {
- [self.userName release];
- [self.friends release];
- [self.address release];
- [self.addresses release];
- [super dealloc];
- }
- - (void)encodeWithCoder:(NSCoder*)coder
- {
- [coder encodeObject:self.userName forKey:SAMPLEDATA_KEY_USERNAME];
- [coder encodeInt:self.age forKey:SAMPLEDATA_KEY_AGE];
- [coder encodeBool:self.gender forKey:SAMPLEDATA_KEY_GENDER];
- [coder encodeObject:self.friends forKey:k_FRIENDS];
- [coder encodeObject:self.address forKey:k_ADDRESS];
- [coder encodeObject:self.addresses forKey:k_userEntity_addresses];
- }
- - (id)initWithCoder:(NSCoder*)decoder
- {
- if (self = [super init])
- {
- if (decoder == nil)
- {
- return self;
- }
- self.userName = [decoder decodeObjectForKey:SAMPLEDATA_KEY_USERNAME];
- self.gender = [decoder decodeBoolForKey:SAMPLEDATA_KEY_GENDER];
- self.age = [decoder decodeIntForKey:SAMPLEDATA_KEY_AGE];
- self.friends = [decoder decodeObjectForKey:k_FRIENDS];
- self.address = [decoder decodeObjectForKey:k_ADDRESS];
- self.addresses = [decoder decodeObjectForKey: k_userEntity_addresses];
- }
- return self;
- }
- @end
[cpp] view plaincopy
- View Code //
- // AddressEntity.m
- // NSCodingDemo
- //
- // Created by on 11-5-20.
- // Copyright 2011 , Inc. All rights reserved.
- //
- #import "AddressEntity.h"
- #define k_Id (@"id")
- #define k_AddresEntity_address (@"addrss")
- @implementation AddressEntity
- @synthesize Id, address;
- -(void)dealloc
- {
- [self.address release];
- [super dealloc];
- }
- - (NSString *)description
- {
- return [NSString stringWithFormat:@"id :%d address: %@" , self.Id, self.address];
- }
- - (void)encodeWithCoder:(NSCoder*)coder
- {
- [coder encodeObject:self.address forKey:k_AddresEntity_address];
- [coder encodeInt:self.Id forKey:k_Id];
- }
- - (id)initWithCoder:(NSCoder*)decoder
- {
- if (self = [super init])
- {
- if (decoder == nil)
- {
- return self;
- }
- self.address = [decoder decodeObjectForKey:k_AddresEntity_address];
- self.Id = [decoder decodeIntForKey:k_Id];
- }
- return self;
- }
- @end
[cpp] view plaincopy
- View Code -(IBAction)write:(id)sender
- {
- //get document path
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *filePath = [documentsDirectory stringByAppendingPathComponent:k_FILE_NAME];
- NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
- NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
- UserEntity *userEntity = [[[UserEntity alloc] init] autorelease];
- userEntity.age = 18;
- userEntity.gender = NO;
- userEntity.userName = @"add";
- NSArray *friends = [[NSArray alloc] initWithObjects:@"ac", @"2", @"3", @"4", nil];
- userEntity.friends = friends;
- AddressEntity *address = [[[AddressEntity alloc] init] autorelease];
- address.Id = 1;
- address.address = @"china";
- userEntity.address = address;
- AddressEntity *address2 = [[[AddressEntity alloc] init] autorelease];
- address2.Id = 2;
- address2.address = @"china2";
- AddressEntity *address3 = [[[AddressEntity alloc] init] autorelease];
- address3.Id = 3;
- address3.address = @"china3";
- NSArray *addresses = [[NSArray alloc] initWithObjects:address2, address3 , nil];
- userEntity.addresses = addresses;
- [archiver encodeObject:userEntity forKey:@"KEY"];
- [archiver finishEncoding];
- BOOL success = [data writeToFile:filePath atomically:YES];
- }
- -(IBAction)read:(id)sender
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *filePath = [documentsDirectory stringByAppendingPathComponent:k_FILE_NAME];
- NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
- NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
- UserEntity *userEntity = [unArchiver decodeObjectForKey:@"KEY"];
- NSLog(@"name is %@ , friend %@ address :%@ , addresses is :%@", userEntity.userName , userEntity.friends , userEntity.address, userEntity.addresses);
- }
时间: 2024-10-14 04:14:29