Objective
All development before IOS5 requires the developer to control the reference and release of their own objects. The modifiers used are assign, copy, retain
After IOS5, Apple introduced the arc (automatic reference counting) mechanism, introduced a new modifier to replace the previous modifier strong, weak
Simple description
In 1:ARC environment, strong instead of Retain.weak instead of assign
2:weak: In the ARC environment, all weak pointers pointing to this object will be set to nil. This t feature is useful, and it is believed that many developers have been plagued by the exc_bad_access of pointers to objects that have been disposed of, and that after using arc, either strong or weak type pointers will no longer point to an object that has been destroyed. It fundamentally solves the crash caused by accidental release.
3:assign: Simple assignment, does not change reference count, applies simple data types to underlying data types (such as nsinteger,cgfloat) and C data types (int, float, double, char, etc.)
4:copy: Create an object with an index count of 1 and then release the old object
5:strong: In an ARC environment, the object is not destroyed as long as an object is pointed to by a strong pointer. If the object is not pointed to by any strong pointer, it will be destroyed. By default, all instance variables and local variables are of type strong. It can be said that the strong type of the pointer in the behavior with non-arc under the retain is more similar
6:retain: In the non-arc era, you need to retain an object that you want to keep, which is not required in the ARC environment. Now the only thing to do is to point to the object with a pointer, and as long as the pointer is not reset to NULL, the object will always be on the heap. When the pointer points to the new value, the original object will be release once. This is useful for instance variables, sunthesize variables, or local variables.
1: What is the difference between assign,copy,retain?
Assign: Simple assignment, without changing the index count (Reference counting).
Copy: An object becomes a new object (new memory address) reference count is 1 The original object count is unchanged
Retain: Frees the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1
2.__weak, __unsafe_unretained, __autoreleasing, the difference
__weak: Declares a weak reference that can be automatically nil.
__unsafe_unretained: Declares a weak application, but does not automatically nil, that is, if the area of memory pointed to is released, the pointer is a wild pointer.
__autoreleasing: A parameter used to modify a function that is automatically freed when the function returns.
The difference between 3:weak and strong:
The survival of the object is directly determined by the existence of strong references, which are often referred to as references. If a reference to an object does not exist, and the object no longer appears in the list, the object is freed from memory.
Weak references are the same as strong references, except that they do not determine the survival of the object. Even if an object is held by countless references, it will be cleared if no strong reference is directed to him.
Simply put, strong equals retain,weak a function more than assign, when the object disappears automatically turns the pointer to nil.
Strong: Is the default keyword.
The difference (weak and strong) is that when an object no longer has a strong type pointer pointing to it, it is freed, even if there is a weak pointer to it.
Once the last strong pointer is left, the object will be released and all remaining weak pointers will be cleared.
There may be an example to describe it as appropriate.
Imagine our object is a dog, the dog wants to run Away (be released).
The strong type pointer is like a tethered dog. As long as you hang the dog with a leash, the dog will not run away. If there are 5 people holding a dog (5 strong pointers pointing to 1 objects), the dog will not run away unless 5 of the ropes fall off.
The weak type pointer is like a child pointing at the dog and shouting: "Look!" A dog is there. "As long as the dog is kept tied, the child can see the dog, and the (weak pointer) will always point to it. As long as the dog's leash comes off, the dog will run away, no matter how many children are watching it.
As long as the last strong pointer no longer points to the object, the object is freed and all weak pointers are cleared.
- Zero-zeroing Weak references
That is, the weak references are set to nil after the object to which the reference points is disposed. To use a zero-zeroing weak reference, you must explicitly declare them. There are two ways to declare a zero weak reference: Declare a variable using the _weak keyword or use the weak attribute on the property (below).
[OBJC] View plain copy print?
__weak NSString *mystring;
@property (weak) NSString *mystring;
If you want to use arc on older systems that do not support weak references, you can use the __unsafe_unretained keyword and the unsafe_unretained attribute, which tells Arc that this particular reference is a weak reference.
There are two naming conventions to be aware of when using arc:
1. Attribute names cannot start with new, such as
[OBJC] View plain copy print?
@property NSString *newstring;
is not allowed.
2. Attributes cannot have only one read-only and no memory management features. If you do not have arc enabled, you can use
[OBJC] View plain copy print?
@property (readonly) NSString *title;
Statement, but if you enable the ARC feature, you must establish who will manage the memory. Because the default feature is assign, you can do a simple fix and use unsafe_unretained.
Usage Scenarios
1: Use assign: for underlying data types (nsinteger,cgfloat) and C data types (int, float, double, char, and so on)
2: Use copy: to NSString
3: Use retain: For other nsobject and its subclasses
4:strong and weak
Strong is used to modify properties of strong references;
@property (Strong) SomeClass * aobject;
corresponding to the original
@property (Retain) SomeClass * aobject; and @property (copy) SomeClass * aobject;
Weak is used to modify the properties of weak references;
@property (weak) SomeClass * aobject;
corresponding to the original
@property (assign) SomeClass * aobject;
5:nonatomic Keywords:
Atomic is a thread-protection technique used by OBJC, basically to prevent the data from being read by another thread when the write is not completed. This mechanism is resource-intensive, so nonatomic is a very good choice on small devices such as the iphone, if there is no communication programming between multiple threads.
Complementary basics
iOS objects are inherited from NSObject, which has a method: Retaincount, memory reference count. Reference counting is used in many techniques: COM components under window, multi-threaded semaphores, read and write locks, and thought alike.
(General: Exceptions are discussed later)
Alloc object is assigned a reference count of 1
Reference count for retain objects +1
Copy copy of an object into a new object (new memory address) reference count of 1 original object count unchanged
Release object reference count-1 if 0 frees memory
Autorelease object reference count-1 if 0 is not released immediately, released at the nearest pool
NSLog (@ "smessage retaincount:%u", [smessage Retaincount]);
The principle of memory management is to balance the final reference count,
Memory leaks If last reference count is greater than 0
If the reference count equals 0 also operates on the object, memory access failure occurs, crash so try to set as nil
Both of these problems are serious, so be sure to pay attention to memory release and set to nil after use
member variables and properties
It's not as simple as it is, you might need to call another function to assign a variable in one function.
There are two choices: class member variables and usage properties
@interface Testmem:nsobject {
Testobject *m_testobject; Member variables
Testobject *testobject; Member variables
}
The member variables are consistent with the memory management above, but the balance of reference count plus and minus is maintained in different functions.
So you have to check each time you assign the last time you've been assigned. Is it possible to invoke
When do I use attributes?
1. Make the members public.
2. Outlet is generally declared as a property (this memory is in the system control, but we should still do the same operation, will speak later)
3. If many functions need to change this object, or the function triggers many times, it is recommended to use attributes. Let's see what it looks like when the property function expands:
//Assign
-(void) Settestobject:(id) newvalue{
testobject= newvalue;
}
//Retain
-(void) Settestobject:(id) newvalue{
if (testobject!= newvalue) {
[testobject release]; testobject= [newvalue retain];
}
}
//Copy
-(void) Settestobject:(id) newvalue{
if (testobject! = newvalue) {
[Testobject release] ;
Testobject = [newvalue copy];
}
}
Asssign relative to the pointer assignment, do not manipulate the reference count, note that the original object is not necessary, be sure to set this to nil
retain equivalent to the original object reference count plus 1
Copy does not change the reference count of the original object, Generates a new object reference count of 1
Note:
The Settestobject method is called by the Self.testobject Lvalue. The right-hand value is the Get method, and the Get method is simpler, needless to say,
and the Testobject uses a member variable Br>self.testobject = [[Testobject alloc] init]; Wrong Reatin two times
Testobject = [Nsarray objectbyindex:0];//Wrong insecure, no retain back release error
If Testobject already has a value also mem leak
Automatically manage objects
IOS provides a lot of static (+) class methods for creating objects, which are statically, and can be directly used with the class name
Calls such as:
NSString *teststring = [nsstring stringwithformat:@ "test"];
TestString is an automatically managed object, you don't have to relese him, he has a big retain count, and the number does not change after release.
Exception
Some of the objects generated by Alloc are automatically managed as follows:
NSString *teststring = [[NSString alloc] initwithstring:@ "test1"];
Retain count is also a large number, no way to release
But for the code to correspond, still should add [teststring release];
Otherwise Xcode's analyze will recognize the memory leak, but instruments leak tool detection is not
Reference Document: Http://www.cnblogs.com/allanliu/p/4941780.html
http://blog.csdn.net/addychen/article/details/39549177
Differences in IOS Property modifiers