copy与mutableCopy

一、对系统自带类的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都进行了深拷贝。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容