@ Summary of copy. strong. weak in property, propertystrongweak
1. Why do NSString attributes use copy?
NSString attributes can be modified with strong, but some problems may occur. Please refer to the following code:
# Import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) NSString * nameStrong; @ property (nonatomic, copy) NSString * nameCopy; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSMutableString * muStr = [NSMutableString stringWithString: @ "zhangsan"]; self. nameCopy = muStr; self. nameStrong = muStr; NSLog (@ "nameStrong = % @ nameCopy = % @", self. nameStrong, self. nameCopy); [muStr appendString: @ "feng"]; NSLog (@ "nameStrong =%@ nameCopy =%@", self. nameStrong, self. nameCopy );}
The output is as follows:
10:10:25. 772 string [619: 12684]Before modifying a variable stringNameStrong = zhangsan nameCopy = zhangsan
10:10:25. 773 string [619: 12684]After modifying a variable stringNameStrong = zhangsanfeng nameCopy = zhangsan
When the variable string changes, the string modified by strong also changes, while the string modified by copy does not change.
Summary: The string modified with strong. When the string of NSMutableString is assigned a value to it, the string of NSMutableString changes, and it also changes, because stong is only a reference to the pointer, and the reference count is increased by 1.
The string modified with copy will not change with the source, because copy will copy one copy.
If the source string is immutable, copy is the same as strong. Because the source is an immutable string, use copy or strong to check your needs, if you want him to use strong as the source changes, and do not want to change the useful copy, it will not be changed, so for security reasons, the string will use copy
2. Proxy. Why does the weak be used for controls that manually drag lines?
Self-> Object (such as person object)-> proxy-> self (when setting Proxy: self. delegate = self );
This will cause a circular reference. weak indicates that this attribute defines a non-owning relationship. When a new value is set for this attribute, neither the new value nor the old value will be released.
First, the weak is not used for the control that is manually dragged because it may cause loop reference. The self-> view-> sunViews-> control already has a strong reference. If the control uses strong, self-> control, which has two strong references. When released, you must cut off two references to the control. If one is not released, the control cannot be released, of course, it's still safer to use strong.
3. Why does block use copy?
The block Method content is in the stack area. First, the stack area is automatically managed by the system and the memory is cleared at any time. When accessed again, it may cause a wild pointer, the copy keyword copies the block content to the heap zone, which is managed by the programmer.