IOS学习笔记二十四(NSData归档多个对象和归档对象实现深复制)

1、NSData归档多个对象

一、之前我写的学习笔记都是归档一个对象,如果需要归档多个对象我们需要借助NSData

二、步骤

      1)、NSMutableData作为参数,构建 NSKeyedArchiver对象

      2)、调用NSKeyedArchiver的encodeXXX

      3)、调用NSKeyedArchiver的finishEncoding方法

      4)、NSMutableData保存到文件

 
2、归档对象实现深复制

   我们知道深复制就是复制对象和原始对象没有任何公用的部分,修改复制对象的值不会对原始对象产生影响。

步骤

1)、NSKeyedArchiver的archivedDataWithRootObject

2)、NSKeyedUnarchiver的unarchiveObjectWithData

 
 
3、实现Demo

 IApple.h

    #import <Foundation/Foundation.h>
    #ifndef IApple_h
    #define IApple_h
    @interface IApple : NSObject <NSCoding>
    @property (nonatomic, copy) NSString *color;
    @property (nonatomic, assign) double weight;
    @property (nonatomic, assign) int size;
    -(id)initWithColor:(NSString *) color weight:(double) weight size:(int) size;
    @end
     
    #endif /* IApple_h */

 
IApple.m

    #import  "IApple.h"
    #import <Foundation/Foundation.h>
    @implementation IApple
    @synthesize color = _color;
    @synthesize weight = _weight;
    @synthesize size = _size;
    -(id)initWithColor:(NSString *) color weight:(double) weight size:(int) size
    {
        if (self = [super init])
        {
            self.color = color;
            self.weight = weight;
            self.size = size;
        }
        return self;
    }
    -(NSString *)description
    {
        return [NSString stringWithFormat:@"<IApple [color = %@, weight = %g, _size = %d]>", self.color, self.weight, self.size];
    }

更多请见:http://www.mark-to-win.com/tutorial/52017.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值