copy的概念特点
- copy产生一个新副本的过程,利用一个原对象产生一个新对象
- copy:创建一个不可变的副本(NSString;NSArray;NSDictionary;)
- mutableCopy :创建一个可变的副本 (NSMutableDictionary; NSMutableArray;NSMutableString)
- 修改新文件,不会影响原文件
- 修改原文件,不会影响新文件
- 浅拷贝:如果没有生成新对象我们称为浅拷贝,本质是指针拷贝,指向的还是同一处
- 深拷贝:如果生成了新的对象,我们称为深拷贝,本质就是创建了一个新的对象
NSString * str = @"demo";
NSMutableString *copyStr = [str copy];
NSMutableString *mcopyStr = [str mutableCopy];
NSLog(@"str = %@ copyStr = %@ mcopyStr = %@",str,copyStr,mcopyStr);
NSLog(@"str = %p copyStr = %p mcopyStr = %p",str,copyStr,mcopyStr);
log:
str = demo copyStr = demo mcopyStr = demo
str = 0x10aa760c8 copyStr = 0x10aa760c8 mcopyStr = 0x60000025aca0
结论:
1、copy和mutableCopy拷贝出来的对象中的内容和以前内容一致
2、不可变的字符串通过copy操作,没有生成新的对象,而是指向同一内存
3、不可变的字符串通过mutableCopy操作,生成新的可变对象,所以需要生成一个新对象
代码语言:javascript复制NSMutableString *str1 = [NSMutableString stringWithFormat:@"demo"];
NSMutableString *copyStr1 = [str1 mutableCopy];
NSMutableString *mcopyStr1 = [str1 mutableCopy];
NSLog(@"str = %@ copyStr = %@ mcopyStr = %@",str1,copyStr1,mcopyStr1);
NSLog(@"str = %p copyStr = %p mcopyStr = %p",str1,copyStr1,mcopyStr1);
log:
str = demo copyStr = demo mcopyStr = demo
str = 0x6040002570a0 copyStr = 0x604000257310 mcopyStr = 0x6040002570d0
结论:
1、copy和mutableCopy拷贝出来的对象中的内容和以前内容一致
2、可变的字符串通过copy操作,生成新的对象
3、可变的字符串通过mutableCopy操作,生成新的可变对象
自定义类的实现copy(NSCopying协议)
- 若想令自己写的类具有copy功能,则需要实现NSCopying、NSMutableCopying协议
- (id)copyWithZone:(nullable NSZone *)zone;
- (id)mutableCopyWithZone:(nullable NSZone *)zone;
看个例子
代码语言:javascript复制#import <Foundation/Foundation.h>
@interface Phone : NSObject<NSCopying>
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign,readonly) NSInteger price;
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price;
@end
#import "Phone.h"
@implementation Phone
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price{
self = [super init];
if (self) {
_name = [name copy];
_price = price;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone{
Phone *phone = [[[self class] allocWithZone:zone] initWithName:_name withPrice:_price];
return phone;
}
Phone *p = [[Phone alloc]initWithName:@"iPhone" withPrice:999];
NSLog(@"%p--%@--%zd",p,p.name,p.price);
Phone *p1 = [p copy];
NSLog(@"%p--%@--%zd",p1,p1.name,p1.price);
log:
0x60000002cce0--iPhone--999
0x6000000371c0--iPhone--999
注:在- (id)copyWithZone:(NSZone *)zone方法中,一定要通过[self class]方法返回的对象调用allocWithZone:方法。因为指针可能实际指向的是PersonModel的子类。这种情况下,通过调用[self class],就可以返回正确的类的类型对象。