Copy, retain, assign, ReadOnly, Readwrite,strong,weak,nonatomic finishing

Source: Internet
Author: User

Copy: Create an object with an index count of 1 and then release the old object to NSString
For NSString it states that a copy of the passed-in value is used when the value is assigned. Copy work is performed by the copy method, and this property is valid only for those object types that implement the Nscopying protocol. For a more in-depth discussion, refer to the "Copying" section.


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
For other NSObject and its subclasses
Release the old value of the parameter, and then retain the new value
Specifies that retain will wake the retain message of the incoming value when the value is assigned. This property can be used only for Objective-c object types, not for core Foundation objects. (for obvious reasons, retain increases the object's reference count, and neither the base data type nor the core Foundation object has a reference count-the translator's note).
Note: When you add an object to an array, the reference count increases the number of references to the object by +1.

The actual syntax for retain is:
-(void) SetName: (NSString *) NewName {
if (name! = NewName) {
[Name release];
name = [NewName retain];
Name ' s retain count have been bumped up by 1
}
}

Release the old value of the parameter, and then retain the new value
Specifies that retain will wake the retain message of the incoming value when the value is assigned. This property can be used only for Objective-c object types, not for core Foundation objects. (for obvious reasons, retain increases the object's reference count, and neither the base data type nor the core Foundation object has a reference count-the translator's note).
Note: When you add an object to an array, the reference count increases the number of references to the object by +1.
The actual syntax for retain is:
-(void) SetName: (NSString *) NewName {
if (name! = NewName) {
[Name release];
name = [NewName retain];
Name ' s retain count have been bumped up by 1
}
}
Copy and retain:
Copy actually creates an identical object, and retain is not:
such as a NSString object, the address is 0x1111, the content is @ "STR"
Copy to another nsstring, the address is 0x2222, the content is the same, the new object retain is 1, the old object has not changed
Retain to another nsstring, the same address (set up a pointer, pointer copy), the content of course the same, the object's retain value +1
That is, retain is a pointer copy and copy is a copy of the content. Wow, it's a lot easier than you think.

The set method of retain should be shallow copy, copy's set method should be deep copy
Copy another usage:
Copy is a copy of the content, which is true for like NSString.
But what if copy is a Nsarray? for example,
Nsarray *array = [Nsarray arraywithobjects:@ "Hello" @ "World" @ "Baby"];
Nsarray *array2 = [array copy];
At this point, the system does open up a memory space for array2, but what we realize is that each element in the array2 is simply a copy of a pointer to the corresponding element in the array. This is called "shallow copy."

Assign: Simple assignment, not changing index count
Simple data types for underlying data types (such as nsinteger,cgfloat) and C data types (int, float, double, char, etc.)
This flag indicates that the setting is assigned directly, which is also the default value. In an application that uses garbage collection, if you want a property to use assign, and this class conforms to the nscopying protocol, you should explicitly indicate this tag instead of simply using the default value, otherwise you will get a compile warning. This again explains to the compiler that you really need to assign a value, even if it is a copy.


Assign and retain:
1. Contact C, then assume that you allocate a piece of memory with malloc, and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a value to (assign) B. At this time A and B point to the same piece of memory, I ask when a no longer need this memory, can you directly release it? The answer is no, because a does not know whether B is still using this memory, and if A is released, then B will cause the program to crash when it uses this memory.
2. Understand the problem of assign in 1, then how to solve? The simplest method is to use a reference count (reference counting), or the above example, we set a reference count for that memory, and when the memory is assigned and assigned to a, the reference count is 1. The reference count increases to 2 when a is assigned to B. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0, the memory is no longer referenced by any pointers, and the system can release it directly.
Summary: The above two is actually the difference between assign and retain, assign is the direct assignment, which may cause 1 of the problem, when the data is int, float and other native types, you can use assign. Retain as described in 2, using a reference count, retain causes a reference count plus 1, release causes a reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called and memory is recycled.

NSString *pt = [[NSString alloc] initwithstring:@ "abc"];
The preceding section of code performs the following two actions
1 Allocate a piece of memory on the heap to store @ "abc" such as: Memory address: 0x1111 content is "abc"
2 Allocate a piece of memory on the stack to store PT for example: Address: 0Xaaaa Content Naturally 0x1111
Below see the Assign retain copy
Assign situation: NSString *NEWPT = [pt assing];
At this point NEWPT and PT are exactly the same address is 0XAAAA content 0x1111 that is newpt is just the alias of PT, for any one operation is equal to another operation. Therefore Retaincount does not need to increase.
Retain situation: NSString *NEWPT = [pt retain];
At this point the address of NEWPT is no longer 0Xaaaa and may be 0Xaabb but the content is still 0x1111. So both NEWPT and PT can manage the memory where "ABC" is located. So Retaincount needs to increase by 1.
Case of copy: NSString *NEWPT = [pt copy];
At this time, the heap will be re-opened a memory storage @ "ABC" such as 0x1122 content is @ "ABC will also be on the stack for the NEWPT allocation of space such as address: 0XAACC content is 0x1122 so retaincount add 1 for NEWPT to manage 0x1122 this memory

ReadOnly This tag indicates that the property is read-only, the default tag is read-write, and if you specify read-only, only one reader is required in @implementation. Or if you use the @synthesize keyword, there is also a reader method that is parsed. And if you try to assign a value to a property using the dot operator, you'll get a compilation error.


ReadWrite This tag indicates that the property is read-write, which is also the default property. Both the setup and the reader need to be implemented in @implementation. If you use the @synthesize keyword, both the reader and the setup are parsed.


Nonatomic: Non-atomic access, when assigning values to attributes without locking, multi-threaded concurrent access improves performance. If this attribute is not added, the default is two access methods are atomic transaction access.

Above from Http://hi.baidu.com/wolf_childer/item/3a0f1affb238a11ce2e3bdb0

Weak and strong property (the difference between strong and weak references)

The weak and strong properties are only required when you open arc, and you cannot use retain release autorelease because ARC will do the work for you automatically, but you need to use weak and strong on object properties. Where strong is equivalent to the Retain attribute, and weak is equivalent to assign.

There is only one situation where you need to use weak (the default is strong), just to avoid retain cycles (that is, the parent class has a subclass {parent class retain the subclass}, and the child class calls the parent class {subclass and retain the parent class}, which cannot be release)

ARC is a new feature of iOS 5, called arc (Automatic Reference counting). Simply put, it is the code that automatically joins the Retain/release, and the code that originally needed to manually add a reference count to handle memory management can be automatically completed by the compiler.
This function is started in IOS 5/mac OS X 10.7 and can be used later with Xcode4.2.

The strong and weak in property

Strong keyword and retain, using it, reference counting automatic +1, with an example to explain everything

@property (nonatomic, strong) NSString *string1;

@property (nonatomic, strong) NSString *string2;


There are two of these properties,


@synthesize string1;

@synthesize string2;




Guess what results The following code will output?


Self.string1 = @ "String 1";

Self.string2 = self.string1;

Self.string1 = nil;

NSLog (@ "String 2 =%@", self.string2);



The result is: string 2 = string 1

Since string2 is a strong-defined property, the reference count of +1 makes them point to the value @ "String 1", which is not difficult to understand if you are familiar with retain.


Then we look at the Weak keyword:

If this declares two properties:


@property (nonatomic, strong) NSString *string1;

@property (nonatomic, weak) NSString *string2;


and define

@synthesize string1;

@synthesize string2;


Let's guess, what is the output below?


Self.string1 = @ "String 1";

Self.string2 = self.string1;

Self.string1 = nil;

NSLog (@ "String 2 =%@", self.string2);


The result is: String 2 = null


Analysis, because Self.string1 and Self.string2 point to the same address, and string2 no retain memory address, and Self.string1=nil freed memory, so string1 is nil. A pointer that is declared as weak, the pointer to the address once released, these pointers will be assigned to nil. Such a benefit can effectively prevent the wild hands. Why does Daniel say that when the pointer space is released, the pointer will be NULL when it is developed in C + +. Here we do this step with the weak keyword.

Above from http://www.cnblogs.com/mybkn/archive/2012/03/08/2384860.html

Some of the UI's property should use retain, strong, or weak.

The property for some UI should use retain, strong, or weak.
1, in the Stanford University of the video to see that the use of WEAK,IOS5 will help you do the rest, even the release is not used, dealloc are not overloaded (perhaps I did not look so serious, it feels like this).
2, in the company's project code often see the use of retain
3, declare using @property, also need to declare the instance variable (interface below the two curly braces). I don't think I saw it on the Stanford video. Use instance variables (see the third one now).
4, the current popular iphone4 and iphone4s is the use of iOS4? The new features of IOS5 should not be used in order to be compatible.

1, for retain, generally refers to the pointer, these properties need to save the reference count, to prevent the situation of zombies, at that time for the NSString type, this is not retain, but copy, but for strong, these you do not need to consider, It will decide whether to choose retain or copy, and for assign, a non-pointer variable, such as Nsinteger and so on, there is the time to avoid the circular reference, for weak, and assign almost, but it is a little more, is that, It automatically sets the type variable to nil.
2, as for the company often use retain reason, one is due to coding habits, there is the project needs.
3, whether you need to declare the instance variable, which depends on the special case, if you do not declare the instance variable in interface, but you in the. m file in the @synthesize, that time actually indicates that you have declared this instance variable.
4, for this problem, the system can be upgraded, and there is no stipulation iphone4s can only use IOS4.

1. Specific point: Iboutlet can be weak,nsstring for copy,delegate General for the weak, others see the situation. In general, the properties of the class "internal" are set to strong, and the properties of the class "external" are set to weak. In the end it is a question of attribution. Be careful that circular references cause memory to fail to release.
2. You will see a lot of Retian without arc.
3. If you write @synthesize ABC = _ABC, then the system automatically helps you to declare a _ABC instance variable.

Using assign: for underlying data types (Nsinteger) and C data types (int, float, double, char, etc.)
Using copy: On NSString
Use retain: For other nsobject and its subclasses

How to understand the strong and weak in objective-c?

The strong-modified property invokes the Retain method that points to the object when the value is assigned, causing its reference count to be added by 1.
The weak will not.

There is also a unsafe_unretained, similar to weak, the difference is that the point object disappears when it does not "automatically" become nil.

(go) Copy, retain, assign, ReadOnly, Readwrite,strong,weak,nonatomic finishing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.