iOS development: Strong, weak and other detailed details

Source: Internet
Author: User

Now let's look at the New keyword in iOS5 strong, weak, unsafe_unretained. can be used to correspond to the previous keyword learning strong and retain similar, weak and unsafe_unretained functions almost (a bit different, and so on, the two new keywords and assign similar). With these new keywords in iOS5, you don't have to manage your memory manually, and programmers from other languages like Java are very helpful.

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

@property (nonatomic, strong) NSString *string1;

@property (nonatomic, strong) NSString *string2;

Have such two attributes

@synthesize string1;

@synthesize string2;

Guess what the following code will print out?

Self.string1 = @ "String 1";

[self.string2 = self.string1;

[self.string1 = nil;

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

The result: string 2 = string 1

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

Then we'll look at the weak keyword:

If you declare two properties like this:

@property (nonatomic, strong) NSString *string1;

@property (nonatomic, weak) NSString *string2;

and define

@synthesize string1;

@synthesize string2;

And guess what the following output is?

Self.string1 = [[NSString alloc] initwithutf8string: "String 1"];

Elf.string2 = self.string1;

Self.string1 = nil;

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

The result: String 2 = null

Analysis, because Self.string1 and Self.string2 point to the same address, and string2 does not have retain memory address, and Self.string1=nil freed memory, so string1 for nil. A pointer that is declared as a weak, and the pointer points to an address that, once released, will be assigned to nil. Such benefits can be effective in preventing wild pointers. In the development process of C + +, why Daniel said that when the space of the pointer is released, the pointer will be assigned NULL. Here's a weak keyword to help us make this move.

Then we'll see unsafe_unretained.

From the name can be seen, unretained and unsafe, because it is unretained and weak a bit similar, but it is unsafe, what is unsafe, look at the example below.

If you declare two properties like this:

and define

@property (nonatomic, strong) NSString *string1;

@property (nonatomic, unsafe_unretained) NSString *string2;

And guess what the following code will do?

Self.string1 = [[NSString alloc] initwithutf8string: "String 1"];

Self.string2 = self.string1;

Self.string1 = nil;

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

Please note that I did not ask you to guess what the output would be, because there would be no output at all, and your program would be crash off. The reason is what, in fact, is caused by wild pointers, so the wild pointer is terrible. Why does it cause wild pointers? With a 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. The memory that accesses the wild pointer then causes crash. So try to use less unsafe_unretained keywords.

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.