Dark Horse Programmer---ios development, property pointers in iOS5 some of the instructions

Source: Internet
Author: User

In the development process of iOS, the definition of attributes is often related to retain, assign, copy, I think we are very familiar with this, I do not introduce, there are many related articles on the Internet.

Now let's look at the New keyword in iOS5 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 can use them without having to manage your memory manually, and programmers who turn from other languages like Java are very useful.


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.


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
@property (nonatomic, strong) NSString *string1;
@property (nonatomic, unsafe_unretained) NSString *string2;

Guess what happens with the code below?
Self.string1 = @ "String 1";
Self.string2 = self.string1;
Self.string1 = nil;
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.

__strong nsstring *yourstring = @ "Your String";
__weak nsstring *mystring = yourstring;
yourstring = nil;
__unsafe_unretained nsstring *theirstring = myString;
Now all the pointers are nil

Look at one more:

__strong nsstring *yourstring = @ "Your String";
__weak nsstring *mystring = yourstring;
__unsafe_unretained nsstring *theirstring = myString;
yourstring = nil;
Now the yourstring and mystring pointers are nil, and theirstring is not nil, but it is a wild pointer.

Introduction to the usage of __autoreleasing:
One of the C/C++,OBJECTIVE-C memory management is: Who assigns who to release. __autoreleasing can make the pair like a delay release. For example, if you want to pass an uninitialized reference to a method, which in this method actually starts the pair, then this will be the time for the __autoreleasing performance. Look at an example:

-(void) generateerrorinvariable: (__autoreleasing nserror * *) paramerror{
Nsarray *objects = [[Nsarray alloc] initwithobjects:@ "A simple Error", nil];
Nsarray *keys = [[Nsarray alloc] initwithobjects:nslocalizeddescriptionkey, nil];
Nsdictionary *errordictionary = [[Nsdictionary alloc] initwithobjects:objects Forkeys:keys];
*paramerror = [[Nserror alloc] initwithdomain:@ "MyApp" Code:1 userinfo:errordictionary];
}
-(void) test
{
Nserror *error = nil;
[Self generateerrorinvariable:&error];
NSLog (@ "error =%@", error);
}

This allows you to apply the space inside the function, even if it is outside the function, and also for who assigns the principle of who is released.

The same is true for the following code, except in cases where arc is not turned on:

-(NSString *) stringtest
{
NSString *retstr = [nsstring stringwithstring:@ "test"];

return [[Retstr retain] autorelease];
}

After opening the arc, you should instead:
-(NSString *) stringtest
{
__autoreleasing nsstring *retstr = [NSString alloc] initwithstring:@ "test"];

return retstr;
}

Dark Horse Programmer---ios development, property pointers in iOS5 some of the instructions

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.