著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
注:
因为NSString是只读的。copy正常应该是要新开辟内存的。
self.copy = copyString;
NSLog(@"%p,%@",copyString,copyString);
NSLog(@"%p,%@",self.copy,self.copy);
[copyString appendString:@"xxxx"];
NSLog(@"%p,%@",copyString,copyString);
NSLog(@"%p,%@",self.copy,self.copy);
2013-12-04 16:35:05.863 Camera[1589:a0b] 0x8e272e0,12345
2013-12-04 16:35:05.863 Camera[1589:a0b] 0x8e271f0,12345
2013-12-04 16:35:05.863 Camera[1589:a0b] 0x8e272e0,12345xxxx
2013-12-04 16:35:05.864 Camera[1589:a0b] 0x8e271f0,12345
我试了一下 看上去这里应该是mutablestring的copy才会指向一个新开辟的地址
===========================================================================
The information is now spread across several guides in the documentation. Here's a list of required reading:
The answer to this question now depends entirely on whether you're using an ARC-managed application (the modern default for new projects) or forcing manual memory management.
Assign vs. Weak - Use assign to set a property's pointer to the address of the object without retaining it or otherwise curating it; use weak to have the property point to nil automatically if the object assigned to it is deallocated. In most cases you'll want to use weak so you're not trying to access a deallocated object (illegal access of a memory address - "EXC_BAD_ACCESS
") if you don't perform proper cleanup.
Retain vs. Copy - Declared properties use retain by default (so you can simply omit it altogether) and will manage the object's reference count automatically whether another object is assigned to the property or it's set to nil; Use copy to automatically send the newly-assigned object a -copy
message (which will create a copy of the passed object and assign that copy to the property instead - useful (even required) in some situations where the assigned object might be modified after being set as a property of some other object (which would mean that modification/mutation would apply to the property as well).
Copy vs Assign vs Retain
Assign is for primitive values like BOOL, NSInteger or double. For objects use retain or copy, depending on if you want to keep a reference to the original object or make a copy of it.
In your setter method for the property, there is a simple assignment of your instance variable to the new value,
(void)setString:(NSString*)newString{
string = newString;
}
This can cause problems since Objective-C objects use reference counting, and therefore by not retaining the object, there is a chance that the string could be deallocated whilst you are still using it.
this retains the new value in your setter method. For example: This is safer, since you explicitly state that you want to maintain a reference of the object, and you must release it before it will be deallocated.
(void)setString:(NSString*)newString{
[newString retain];
[string release];
string = newString;
}
this makes a copy of the string in your setter method: This is often used with strings, since making a copy of the original object ensures that it is not changed whilst you are using it.
(void)setString:(NSString*)newString{
if(string!=newString){
[string release];
string = [newString copy];
}
}