一、对系统自带类的copy与mutableCopy
总结:
|
是否深度拷贝 |
imutable的copy |
否 |
imutable的mutableCopy |
是 |
mutable的copy |
是 |
mutable的mutableCopy |
是 |
|
对NSString
|
地址 |
类 |
是否深度拷贝 |
str |
0x100003088 |
__NSCFConstantString |
|
str Copy |
0x100003088 |
__NSCFConstantString |
否 |
str MutableCopy |
0x100102f60 |
__NSCFString |
是 |
对NSMutableString
|
地址 |
类 |
是否深度拷贝 |
mutableStr |
0x100100910 |
__NSCFString |
|
mutableStr Copy |
0x100400240 |
NSTaggedPointerString |
是 |
mutableStr MutableCopy |
0x100400440 |
__NSCFString |
是 |
对NSArray
|
地址 |
类 |
是否深度拷贝 |
array |
0x100400960 |
__NSArrayI |
|
array Copy |
0x100400960 |
__NSArrayI |
否 |
array MutableCopy |
0x100400f30 |
__NSArrayM |
是 |
对NSMutableArray
|
地址 |
类 |
是否深度拷贝 |
mutableArray |
0x100100280 |
__NSArrayM |
|
mutableArray Copy |
0x100100370 |
__NSArrayI |
是 |
mutableArray MutableCopy |
0x100106060 |
__NSArrayM |
是 |
对NSDictionary
|
地址 |
类 |
是否深度拷贝 |
dictionary |
0x100106be0 |
__NSDictionaryI |
|
dictionary Copy |
0x100106be0 |
__NSDictionaryI |
否 |
dictionary MutableCopy |
0x100106d90 |
__NSDictionaryM |
|
是 |
对NSMutableDictionary
|
地址 |
类 |
是否深度拷贝 |
mutableDictionary |
0x100503470 |
__NSDictionaryM |
|
mutableDictionary Copy |
0x1005038d0 |
__NSDictionaryI |
是 |
mutableDictionary MutableCopy |
0x100503910 |
__NSDictionaryM |
是 |
对NSSet
|
地址 |
类 |
是否深度拷贝 |
set |
0x1003039c0 |
__NSSetI |
|
set Copy |
0x1003039c0 |
__NSSetI |
否 |
mutableSetMutableCopy |
0x100303c20 |
__NSSetM |
是 |
对NSMutableSet
|
地址 |
类 |
是否深度拷贝 |
set |
0x103000140 |
__NSSetM |
|
set Copy |
0x100202680 |
__NSSetI |
是 |
mutableSetMutableCopy |
0x100202b50 |
__NSSetM |
是 |
二、自定义类的copy与mutableCopy
// Person.h
typedef NS_ENUM(NSInteger,PersonSex) {
PersonSexMan,
PersonSexWomen
};
@interface Person : NSObject<NSCopying,NSMutableCopying>
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,assign) PersonSex sex;
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age sex:(PersonSex)sex;
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age sex:(PersonSex)sex;
// Person.m
@implementation Person
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age sex:(PersonSex)sex{
if (self = [super init]) {
_name = [name copy];
_age = age;
_sex = sex;
}
return self;
}
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age sex:(PersonSex)sex{
return [[self alloc]initWithName:name age:age sex:sex];
}`
- (id)copyWithZone:(NSZone *)zone{
Person *person = [[[self class] allocWithZone:zone] initWithName:_name age:_age sex:_sex];
return person;
}
- (id)mutableCopyWithZone:(NSZone *)zone{
Person *person = [[[self class] allocWithZone:zone] initWithName:_name age:_age sex:_sex];
return person;
}
@end
对person进行copy和mutableCopy
Person *person = [[Person alloc] initWithName:@"hao" age:23 sex:PersonSexMan];
Person *personCopy = [person copy];
Person *personMutableCopy = [person mutableCopy];
NSLog(@"person: %p",person);
NSLog(@"personCopy: %p",personCopy);
NSLog(@"personMutableCopy: %p",personMutableCopy);
输出:
person: 0103200050
personCopy: 0x103200140
personMutableCopy: 0x103200200
copy与mutableCopy都进行了深拷贝。