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操作,生成新的可变对象,所以需要生成一个新对象
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操作,生成新的可变对象
- (id)copyWithZone:(nullable NSZone *)zone;
- (id)mutableCopyWithZone:(nullable NSZone *)zone;
看个例子
#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],就可以返回正确的类的类型对象。