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];
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交