Features in the Objective-c

Source: Internet
Author: User

One, retain, copy, assign difference

Concept:
Assign: Simple assignment, without changing the index count (reference counting).
Copy: Create an object with an index count of 1 and then release the old object
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

Cases:

1 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 assign];
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

Understand:
1, suppose 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 point A and B points to the same piece of memory, when a does not need to use this memory is not directly released, because a does not know that B is also using this memory, if a is released, then B in the use of this memory when the program will crash out
2, in order to solve the problem in 1, the simplest way is to use the reference count, in the above example, we give the block of memory a reference count, when the memory is allocated 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.
3. Above two points is actually the difference between assign and retain, assign is directly assigned, which may cause 1 problems, 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.
4.copy is used when you don't want a and b to share a piece of memory. A and B each have their own memory.

Summarize:
Use assign: for underlying data types (nsinteger,cgfloat) and C data types (int, float, double, char, and so on)
Using copy: On NSString
Use retain: For other nsobject and its subclasses

Second, the strong,week,unsafe_unretained of new RAC in iOS5

Description
IOS5 in the new keyword strong, weak, unsafe_unretained. can be corresponding to the previous keyword learning strong and retain similar, weak and unsafe_unretained function almost (a bit different, and so on, these two new keywords and assign similar). With these new keywords in iOS5, you don't have to manage your memory manually.
Specific use:
Strong keyword and retain, using it, reference counting automatic +1, with an example to explain everything

1. @property (nonatomic, strong) NSString *string1;
2. @property (nonatomic, strong) NSString *string2;

Note: nonatomic keywords:

Noatomic 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.

There are two of these properties,

1. @synthesize string1;
2. @synthesize string2;

Guess what results The following code will output?

1. self.string1 = @ "String 1";
2. self.string2 = self.string1;
3. self.string1 = nil;
4. 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:

1. @property (nonatomic, strong) NSString *string1;
2. @property (nonatomic, weak) NSString *string2;

and define

1. <pre name= "code" class= "CPP" > @synthesize string1;
2. @synthesize string2;

Let's guess, what is the output below?

1. self.string1 = @ "String 1";
2. self.string2 = self.string1;
3. self.string1 = nil;
4. 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.

Then we'll see unsafe_unretained.
From the name can be seen, unretained and unsafe, because it is unretained so and weak a bit similar, but it is unsafe, what is unsafe, see the example below.
If this declares two properties:
and define
1. @property (nonatomic, strong) NSString *string1;
2. @property (nonatomic, unsafe_unretained) NSString *string2;

Guess what happens with the code below?
1. self.string1 = @ "String 1";
2. self.string2 = self.string1;
3. self.string1 = nil;
4. NSLog (@ "String 2 =%@", self.string2);

Please note that I am not asking you to guess what the output will be because there will be no output at all, and your program will crash out.

The reason is, in fact, the wild pointer caused, so the wild pointer is terrible. Why do they create wild pointers? As with the pointer declared with unsafe_unretained, the Self.string1=nil has freed the memory, but string2 does not know that it has been released, so it is a wild pointer.  Then accessing the memory of the wild pointer causes crash. So try to use less unsafe_unretained keywords.

Strong,weak, unsafe_unretained are often used to declare attributes, and if you want to declare a temporary variable you have to use __strong, __weak, __unsafe_unretained, __autoreleasing, The usage is similar to that described above.
Let's take a look at examples.

1. __strong NSString *yourstring = @ "Your String";
2. __weak NSString *mystring = yourstring;
3. yourstring = nil;
4. __unsafe_unretained NSString *theirstring = myString;
5.//Now all pointers are nil

Look at one more:

1. __strong NSString *yourstring = @ "Your String";
2. __weak NSString *mystring = yourstring;
3. __unsafe_unretained NSString *theirstring = myString;
4. yourstring = nil;
5.//Now the yourstring and mystring pointers are nil, and theirstring is not nil, but is a wild pointer.

Features in the Objective-c

Related Article

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.