Objective-c - Why does the copy property in Object c return an object that is not a deep copy?
伊谢尔伦
伊谢尔伦 2017-05-02 09:35:32
0
2
688

Object declared as

@interface MyClass : NSObject
@property (atomic, copy) NSMutableString *name;
@end

is defined as

@implementation MyClass
@synthesize name;
@end

Call

MyClass *m = [[MyClass alloc] init];
NSMutableString *s = [[NSMutableString alloc] initWithString:@"Hello"];
[m setName:s];
NSLog(@"%p", [m name]);
NSLog(@"%p", [m name]);
NSLog(@"%p", [m name]);

If the object returned by copy each time is a deep copy of the variable string, why are the printed addresses the same?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
过去多啦不再A梦

copy是指在赋值的时候进行一次copy操作,你可以再尝试打印下s的地址,
ps: mutable类型的属性别用copy

洪涛

Uh-huh. . You printed the same address three times, how can you change it?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template