IOS development: strong, weak, etc.

Source: Internet
Author: User

IOS development: strong, weak, etc.

Now let's look at the New Keyword strong, weak, unsafe_unretained in iOS5. it can be similar to learning strong and retain corresponding to previous keywords. weak and unsafe_unretained functions are similar (a little different. We will introduce them later. These two new keywords are similar to assign ). With these new keywords in iOS5, you don't need to manually manage the memory. programmers who transfer from other languages such as java are very useful.

The strong keyword is similar to the retain keyword. When it is used, the reference count is automatically + 1, and the instance can better describe everything.

@ Property (nonatomic, strong) NSString * string1;

@ Property (nonatomic, strong) NSString * string2;

There are two attributes

@ Synthesize string1;

@ Synthesize string2;

Guess what results will the following code output?

Self. string1 = @ "String 1 ";

[Self. string2 = self. string1;

[Self. string1 = nil;

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

Result: String 2 = String 1

Because string2 is a property defined by strong, reference count + 1 makes them all point to @ "String 1". If you are familiar with retain, This is not difficult to understand.

Next let's look at the keyword weak:

If two attributes are declared as follows:

@ Property (nonatomic, strong) NSString * string1;

@ Property (nonatomic, weak) NSString * string2;

And define

@ Synthesize string1;

@ Synthesize string2;

What is the output below?

Self. string1 = [[NSString alloc] initwithuf8string: "string 1"];

Elf. string2 = self. string1;

Self. string1 = nil;

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

Result: String 2 = null

For analysis, string1 is nil because self. string1 and self. string2 point to the same address and string2 does not have a retain memory address, while self. string1 = nil releases the memory. The pointer declared as weak. Once the address pointed to by the pointer is released, these pointers will be assigned as nil. This benefit effectively prevents wild pointers. In the c/c ++ development process, why do Daniel say that when the pointer space is released, the pointer should be assigned NULL. here we use the weak keyword to help us with this step.

Next let's look at unsafe_unretained.

From the name, we can see that unretained and unsafe are similar to weak because it is unretained, but it is unsafe. What is unsafe? Let's look at the instance below.

If two attributes are declared as follows:

And define

@ Property (nonatomic, strong) NSString * string1;

@ Property (nonatomic, unsafe_unretained) NSString * string2;

Let's guess, what will happen to the following code?

Self. string1 = [[NSString alloc] initwithuf8string: "string 1"];

Self. string2 = self. string1;

Self. string1 = nil;

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

Please note that I have not told you to guess what output will be, because there will be no output at all, and your program will crash. What is the reason? It is actually caused by a wild pointer, so it is terrible. Why does it cause a wild pointer? Similar to the pointer declared by unsafe_unretained, string2 does not know it has been released because self. string1 = nil has released the memory, so it is a wild pointer. Then the access to the memory of the wild pointer causes crash. Therefore, use the unsafe_unretained keyword as little as possible.

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.