分可变对象和不可变对象来说吧,原先我以为和是不是strong有关系,后来测试发现不管是不是strong的对象,传入的形参默认都是拷贝内容相同的内存地址的。只有可变对象实参和形参共用内存地址。
貌似主要的就说完了
先说不可变对象,以NSString为例:
@property(nonnull,strong)NSString*testStr;



可以看到因为形参用的是新的内存地址,在地址内内容改变并不会影响到实参,因为他们的地址不同
然后可变对象,以NSMutableString为例:
@property(nonnull,strong)NSMutableString*testStr;



因为用的是相同的内存地址所以形参改变会影响到实参,这时候想要不改变实参的话可以用copy方法,这样形参就会拷贝一份内容相同,放倒新地址里面。[self editTestStr:[_testStr copy]];这样就可以用新地址了。
如果有人可以告诉我什么可变对象不拷贝指针的话,那就太感谢了
以上
网友评论