ios开发归档与接档

  1. #pragma mark - NSCoding  
  2. - (void)encodeWithCoder:(NSCoder *)aCoder {  
  3.     [aCoder encodeObject:_name forKey:kNameKey];  
  4.     [aCoder encodeInteger:_age forKey:kAgeKey];  
  5.     [aCoder encodeObject:_address forKey:kAddress];  
  6.     [aCoder encodeObject:_photo forKey:kPhotoKey];  
  7. }  
  8.   
  9. - (id)initWithCoder:(NSCoder *)aDecoder {  
  10.     if (self = [super init]) {  
  11.         _name = [aDecoder decodeObjectForKey:kNameKey];  
  12.         _age = [aDecoder decodeIntegerForKey:kAgeKey];  
  13.         _address = [aDecoder decodeObjectForKey:kAddress];  
  14.         _photo = [aDecoder decodeObjectForKey:kPhotoKey];  
  15.     }  
  16.     return self;  
  17. }  
  18.   
  19. #pragma mark - NSCoping  
  20. - (id)copyWithZone:(NSZone *)zone {  
  21.     Archive *copy = [[[self class] allocWithZone:zone] init];  
  22.     copy.name = [self.name copyWithZone:zone];  
  23.     copy.age = self.age;  
  24.     copy.address = [self.address copyWithZone:zone];  
  25.     copy.photo = self.photo;  
  26.     return copy;  
  27. }  
  28. @end 

  1. //保存图片与归档  
  2. - (IBAction)save:(id)sender {  
  3.       
  4.     //准备数据  
  5.     NSString *name = @"小杨在玩iOS";  
  6.     NSInteger age = 22;  
  7.     NSString *address = @"你猜我在哪~";  
  8.     UIImage *photo = [UIImage imageNamed:@"loginman.jpg"];  
  9.     //存储数据到类  
  10.     Archive *archivingData = [[Archive alloc] init];  
  11.     archivingData.name = name;  
  12.     archivingData.age = age;  
  13.     archivingData.address = address;  
  14.     archivingData.photo = photo;  
  15.       
  16.     //归档  
  17.     NSMutableData *data = [[NSMutableData alloc] init];  
  18.     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  
  19.   
  20.     [archiver encodeObject:archivingData forKey:kArchivingDataKey]; // archivingDate的encodeWithCoder  
  21. 方法被调用  
  22.     [archiver finishEncoding];  
  23.     //写入文件  
  24.     [data writeToFile:self.archivingFilePath atomically:YES];  


  1. //接档
  2. - (IBAction)loadArchive:(id)sender {  
  3.     NSData *data = [[NSMutableData alloc] initWithContentsOfFile:self.archivingFilePath];  
  4.     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  5.       
  6.     //获得类  
  7.     Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];// initWithCoder方法被调用  
  8.     [unarchiver finishDecoding];  
  9.       
  10.     //读取的数据  
  11.     NSString *name = archivingData.name;  
  12.     NSInteger age = archivingData.age;  
  13.     NSString *address = archivingData.address;  
  14.     self.imageView.image = archivingData.photo;  
  15.     NSLog(@"%@||%d||%@",name,age,address);  
  16. }
转自:http://www.cnblogs.com/ios8/p/ios-archive.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值