//获取根目录 NSString *homeDictionary = NSHomeDirectory(); //添加储存的文件名 NSString *homePath = [homeDictionary stringByAppendingPathComponent:@"atany.archiver"]; //归档一个字符串 BOOL flag = [NSKeyedArchiver archiveRootObject:@"归档" toFile:homePath];
NSString* str=[NSKeyedUnarchiver unarchiveObjectWithFile:homePath];
//准备数据 CGPoint point = CGPointMake(1.0, 2.0); NSString *info = @"坐标原点"; NSInteger value = 10; //获取Home目录并创建multi.archiver文件 NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"multi.archiver"]; //创建NSMutableData对象 用来存储归档数据 NSMutableData *data = [[NSMutableData alloc]init]; NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; //对多个对象进行归档 [archvier encodeCGPoint:point forKey:@"kPoint"]; [archvier encodeObject:info forKey:@"kInfo"]; [archvier encodeInteger:value forKey:@"kValue"]; [archvier finishEncoding]; //写入文件 [data writeToFile:multiHomePath atomically:YES];
NSMutableData *dataR = [[NSMutableData alloc]initWithContentsOfFile:multiHomePath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dateR]; CGPoint pointR = [unarchiver decodeCGPointForKey:@"kPoint"]; NSString *infoR = [unarchiver decodeObjectForKey:@"kInfo"]; NSInteger valueR = [unarchiver decodeIntegerForKey:@"kValue"]; [unarchiver finishDecoding]; NSLog(@"%f,%f,%@,%d",pointR.x,pointR.y,infoR,valueR);
#import <Foundation/Foundation.h> @interface Archive : NSObject @property (copy,nonatomic) NSString *name; @property NSInteger age; @property (copy,nonatomic) NSString *address; @property (copy,nonatomic) UIImage *photo; @end
#import "Archive.h" #define kNameKey @"NameKey" #define kAgeKey @"AgeKey" #define kAddress @"AddressKey" #define kPhotoKey @"PhotoKey" @implementation Archive @synthesize name = _name; @synthesize age = _age; @synthesize address = _address; @synthesize photo = _photo; #pragma mark - NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_name forKey:kNameKey]; [aCoder encodeInteger:_age forKey:kAgeKey]; [aCoder encodeObject:_address forKey:kAddress]; [aCoder encodeObject:_photo forKey:kPhotoKey]; } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { _name = [aDecoder decodeObjectForKey:kNameKey]; _age = [aDecoder decodeIntegerForKey:kAgeKey]; _address = [aDecoder decodeObjectForKey:kAddress]; _photo = [aDecoder decodeObjectForKey:kPhotoKey]; } return self; } #pragma mark - NSCoping - (id)copyWithZone:(NSZone *)zone { Archive *copy = [[[self class] allocWithZone:zone] init]; copy.name = [self.name copyWithZone:zone]; copy.age = self.age; copy.address = [self.address copyWithZone:zone]; copy.photo = self.photo; return copy; } @end
//保存图片与归档 - (IBAction)save:(id)sender { //准备数据 NSString *name = @"Exile"; NSInteger age = 22; NSString *address = @"Address"; UIImage *photo = [UIImage imageNamed:@"login.jpg"]; //存储数据到类 Archive *archivingData = [[Archive alloc] init]; archivingData.name = name; archivingData.age = age; archivingData.address = address; archivingData.photo = photo; //归档 NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; // archivingDate的encodeWithCoder 方法被调用 [archiver encodeObject:archivingData forKey:kArchivingDataKey]; [archiver finishEncoding]; //写入文件 [data writeToFile:self.archivingFilePath atomically:YES]; }
- (IBAction)loadArchive:(id)sender { NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //获得类 //initWithCoder方法被调用 Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey]; [unarchiver finishDecoding]; //读取的数据 NSString *name = archivingData.name; NSInteger age = archivingData.age; NSString *address = archivingData.address; self.imageView.image = archivingData.photo; NSLog(@"%@||%d||%@",name,age,address); }