copy和mulcopy

1.字符串的内存

   NSString*string11 =@"1";

    NSString*string22 =@"1";

    NSArray*arr11 =@[@"1"];

    NSArray*arr22 =@[@"1"];

string11:0x107e7d108;string22:0x107e7d108--arr11:0x107e7d108;arr22:0x107e7d108

可以看到虽然这是四个对象,但是由于字符串的优化其内存地址是一样的

2.copy

1.不可变类型的copy

NSString*string1 =@"haha";

 NSString*string2 = [string1 copy];

string1:0x10a1010f8,string2:0x10a1010f8

2.可变类型的copy

 NSMutableString *mString1 = [NSMutableString stringWithString:@"hehe"];

NSString*mString2 = [mString1 copy];

mString1:0x60000024e160,mString2:0xa000000656865684

结论copy对于不可变类型是浅拷贝,对于可变类型是深拷贝且可变类型变为不可变类型

3.mulcopy

1.不可变类型的mulcopy

 NSString *string1 = @"haha";

NSMutableString *string3 = [string1 mutableCopy];

[string3 appendString:@"hehe"]; //注意string3变为可变数组

string1:0x1005070f8,string3:0x604000048940

2.可变类型的mulcopy

NSMutableString *mString1 = [NSMutableString stringWithString:@"hehe"];

  NSMutableString*mString3 = [mString1mutableCopy];

   [mString3 appendString:@"haha"];

mString1:0x60000044bf10,mString3:0x60000044c030

结论,mulcopy的结果是mulcopy类型,且都是深拷贝

你可能感兴趣的:(copy和mulcopy)