- 不要等到明天,明天太遥远,今天就行动。
须读:看完该文章你能做什么?
一个类如何实现自定义copy,以及注意事项
学习前:你必须会什么?(在这里我已经默认你具备C语言的基础了)
什么类,copy的本质是什么
一、本章笔记
一、自定义类实现copy
1.遵循两个协议 <NSCopying,NSMutableCopying>
2.实现copyWithZone、mutableCopyWithZone方法
3.样式
- (id)copyWithZone:(NSZone *)zone
{
Person *p = [[self class] allocWithZone:zone];
p.age = _age;
p.name = _name;
return p;
}
子类
- (id)copyWithZone:(NSZone *)zone
{
id obj = [[self class] allocWithZone:zone];
id obj = [super copyWithZone:zone];
[obj setHeight:_height];
// 3.返回副本
return obj;
}
二、code
main.m
#pragma mark 20-自定义类实现Copy
#pragma mark 概念
/*
一、自定义类实现copy
1.遵循两个协议 <NSCopying,NSMutableCopying>
2.实现copyWithZone、mutableCopyWithZone方法
3.样式
- (id)copyWithZone:(NSZone *)zone
{
Person *p = [[self class] allocWithZone:zone];
p.age = _age;
p.name = _name;
return p;
}
子类
- (id)copyWithZone:(NSZone *)zone
{
id obj = [[self class] allocWithZone:zone];
id obj = [super copyWithZone:zone];
[obj setHeight:_height];
// 3.返回副本
return obj;
}
*/
#pragma mark - 代码
#import <Foundation/Foundation.h>
#pragma mark 类
#import "Person.h"
#import "Student.h"
#pragma mark - main函数
int main(int argc, const char * argv[])
{
#pragma 1.
// 1.以后想让自定义的对象 能够被copy只需要遵守 <NSCopying> 协议
// 2.实现协议中的 - (id)copyWithZone:(NSZone *)zone
// 3.在 - (id)copyWithZone:(NSZone *)zone 创建一个副本对象,然后将当前对象的值 赋值给副本对象即可
Person *p = [[Person alloc]init];
p.age = 25;
p.name = @"lyh";
NSLog(@"%@",p);
/*
reason: '-[Person copyWithZone:]: unrecognized selector sent to instance 0x100204ac0'
*** First throw call stack:
reason: '-[Person mutableCopyWithZone:]: unrecognized selector sent to instance 0x1001025e0'
*/
// Person *p2 = [p copy];
Person *p2 = [p mutableCopy];
NSLog(@"%@",p2);
Student *s = [[Student alloc]init];
s.age = 11;
s.name = @"lll";
s.height = 1.33;
NSLog(@"%@",s);
// 如果想让子类在copy的时候 保留子类的属性,那么必须重写copyWithZone方法,在该方法中先调用父类创建副本设置值,然后再设置子类特有的值
Student *s2 = [s copy];
NSLog(@"%@",s2);
return 0;
}
Person
>>>.h
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCopying,NSMutableCopying>
@property (nonatomic,assign) int age;
@property (nonatomic,copy) NSString *name;
@end
>>>.m
#import "Person.h"
@implementation Person
- (id)copyWithZone:(NSZone *)zone
{
// 1.创建一个新的对象
Person *p = [[self class] allocWithZone:zone];
// 2.设置当前对象的内容 给新的对象
p.age = _age;
p.name = _name;
// 3.返回新的对象
return p;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
// 1.创建一个新的对象
Person *p = [[self class] allocWithZone:zone];
// 2.设置当前对象的内容 给新的对象
p.age = _age;
p.name = _name;
// 3.返回新的对象
return p;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name = %@, age = %i",_name,_age];
}
@end
Person
>>>.h
#import "Person.h"
@interface Student : Person
@property (nonatomic,assign) double height;
@end
>>>.m
#import "Student.h"
@implementation Student
- (id)copyWithZone:(NSZone *)zone
{
// 1.创建副本
// id obj = [[self class] allocWithZone:zone];
// // 2.设置数据给副本
// [obj setAge:[self age]];
// [obj setName:[self name]];
id obj = [super copyWithZone:zone];
[obj setHeight:_height];
// 3.返回副本
return obj;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name = %@, age = %i,height = %f",[self name],[self age],_height];
}
@end
网友评论