iOS --------- development and assign the difference between weak

The difference between weak and assign the - the proper use of weak, assign

A difference

1. modified variable type of difference
weak can only modify objects. If the modified basic data types, the compiler will complain - "Property with 'weak' attribute must be of object type".
assign may be modified objects and basic data types. When you need to modify the type of object, MRC era use unsafe_unretained. Of course, unsafe_unretained may also have wild pointers, so its name is " the unsafe_ ."

2. whether a field guide of the difference between
weak no wild pointer problem. Weak because the modified release object (the reference count is 0), the pointer will be automatically set to nil, the object again after the message does not collapse. weak are safe.
assign If the object is modified, will produce wild pointer problem; if modified basic data types are safe. Modified release the object, the pointer is not automatically blanking Crash when a message to the subject.

Second, similar

Object types can be modified, but assign modified objects can be problematic.

Third, the summary

assign suitable for basic data types, such as int, float, struct equivalent type, is not available for reference types. Because value types are placed on the stack, advanced out after follow the principle of the system is responsible for managing the stack memory. The reference types will be placed in the heap, we need to manually manage memory or by ARC management.
weak apply to delegate and block and other reference type, does not lead to wild pointer problem, not a circular reference, very safe.

 



Guess you like

Origin www.cnblogs.com/KiVen2015/p/11203605.html