iOS Development's understanding of retain and assign attributes

Source: Internet
Author: User

Original address: http://rd.189works.com/article-36809-1.html

The following text:

When writing a program, for the variable to be defined into the habit of adding retain attributes, but to what effect it has been not very clear, today did a demo, finally understand their meaning.

Like I'm going to define string var:

NSString *var;

When declaring property, the @property (nonamtic,assign) NSString *var;

Declare the attribute as assign, and when the getter and setter methods are generated using @synthsize, we can use the "." operator to assign a value to Var,

When you declare a property as assign, the implementation of the Setter method is this:

-(void) Setter: (nsstring*) str

{

VAR=STR;

}

Var=[[nsstring alloc]initwithstring:@ "AAA"];

When we do this, we don't call the setter method, we just point the Var to the destination address, and then

NSLog (@ "%d", [var retaincount]), the value will be 1.

If we write this: self.var=[[nsstring alloc]initwithstring:@ "AAA"];

The setter method is invoked, but the value of NSLog (@ "%d", [var retaincount]) is still 1.

If we set the property this way:

@property (Nonamtic,retain) NSString *var;

At this point, the auto-generated setter method is this:

-(void) Setter: (nsstring*) str

[Str retain];

[var release];

VAR=STR;

This makes it clear that the method, which is set to the Retain property, adds 1 to the reference count of the value to be assigned when generating the setter method, and then the object that the Var points to. Then the STR is assigned to VAR. The output of the following code is 2, because the retain of the memory area referred to in setteryyif [[NSString alloc]initwithstring:@ "AAA] is added 1, and then Var points to the area, So the retaincount of Var will naturally be 2:

Self.var=[[nsstring alloc]initwithstring:@ "AAA"];

NSLog (@ "%d", [var retaincount]);

This is done to prevent the memory from being freed by transition, for example, [[NSString alloc]initwithstring:@ ' AAA '] This area of memory is not just directed to Var, there is another variable var2 also point to the area, if we do not set the Retain attribute, After executing var=[[nsstring alloc]initwithstring:@ "AAA", both Var and var1 point to a common memory area, but the retaincount of this memory region is 1. If [var2 release] is executed in another place, so [[NSString alloc]initwithstring:@ "AAA"] refers to the memory area of the Retaincount is 0, so this memory area is released, var is also a wild pointer, then the reference to Var will cause a memory error.

However, it is particularly noteworthy that even if we set the attribute of Var to retain, if we were to assign a value to Var, we would use the following form:

Var=[[nsstring alloc]initwithstring:@ "AAA"];

Instead of:

Self.var=[[nsstring alloc]initwithstring:@ "AAA"];

This retain property does not work, preceded by self, and is equivalent to the attribute we define or assign. Retaincount not going to add 1.

My personal understanding of this is that if you do not display the use of "." operator, the setter method is not invoked, just the passing of the pointer.

Today I have some new ideas to add:

"." The operator in OBJC is the invocation of the method, for example: SELF.STR and [self str] are the same. If I declare a method in the. h file:

-(void) method;

So, I can call this method in two ways: [Self method] or Self.method.

In this way, why can we use a form such as self.str to represent a variable, because of the mechanism of variable attribute in OBJC.

As we said earlier, when you define a variable str, add an attribute such as assign or retain, you can then use @synthesize to generate the appropriate setter and getter methods. In this way, for a variable, there is a corresponding assignment method, so for self.str such a way of writing, is actually called Str corresponding setter or Getter method. In other words, the setter or getter message is sent to and Str. STR is now a method name, not just a variable name.

So if we don't declare attributes on a variable, and there's no @synthesize to generate the setter and getter methods, then we can't use self.str in this form, but only str=@ "AAA", or STR1=STR.

So there is our previous conclusion: using Self.str this form is equivalent to calling a setter or getter method. The retain operation is also performed. Without this form, it is useless to invoke the setter or getter method, and the operation of the retain is impossible to talk about.

Turn a different article, speak better:

Recently, I was asked about when to use self. Assignment problem, I summed up, sent to you for reference. If you have any questions, please treatise.

about what time to use self. is actually related to the access method of obj-c, but many people on the Internet also answer this question, so why is it related to the access method? What's it about? And not many people answered it. At the same time on the content of memory management, please see the OBJECTIVE-C Memory Management Summary ~CC Special Edition, some things I do not explain more.

To get to the point, we often see this code in official documents:

MyClass.h

@interface Myclass:nsobject {
MyObject *myobject;
}
@property (nonatomic, retain) MyObject *myobject;
@end

Myclass.m


@synthesize MyObject;

-(ID) init{
if (self = [super init]) {
MyObject * Amyobject = [[MyObject alloc] init];
Self.myobject = Amyobject;
[Amyobject release];
}
return self;
}


Some people ask, why is it so complicated to assign a value? Why add self. ? Directly written self.myobject = [[MyObject alloc] init]; there's nothing wrong with that? Sometimes it seems normal to not add self?

Now let's look at the contents of memory management:

First look at the indirect assignment:

1. Add self.

MyObject * Amyobject = [[MyObject alloc] init]; Amyobject retaincount = 1;
Self.myobject = Amyobject; MyObject Retaincount = 2;
[Amyobject Release];//myobject retaincount = 1;

2. Do not add self.

MyObject * Amyobject = [[MyObject alloc] init]; Amyobject retaincount = 1;
MyObject = Amyobject; MyObject retaincount = 1;
[Amyobject release];//object has been released

And look at the direct assignment:

3. Add self.

Self.myobject = [[MyObject alloc] init]; MyObject Retaincount = 2;

4. Do not add self.

MyObject = [[MyObject alloc] init]; MyObject retaincount = 1;

Now is not a little dizzy, we first to change the code, the official one of the common wording:

MyClass.h

@interface Myclass:nsobject {
MyObject * _MYOBJECT;
}
@property (nonatomic, retain) MyObject *myobject;
@end

Myclass.m

@synthesize myObject = _myobject;

OK, you try again now, if you use Self._myobject = Amyobject; or myObject = Amyobject; You'll get a mistake, for what, here's the Obj-c access method. Frankly speaking is very simple, as we all know, @property (nonatomic, retain) MyObject *myobject; is to set the access method for a property, but we usually use the same method name and property name, now you write it a different name, it will be very clear. _myobject is the property itself, MyObject is the accessor method name.

Now we know that self. is the access method of accessing the attribute, how does the access method work? Self.myobject = [[MyObject alloc] init]; Why is there a memory leak?
I don't explain much about nonatomic, it's not the point I'm going to talk about, and I'm not completely clear about it, not misleading. I only say assign, retain, copy.

The Get method is:

-(myobject*) myobject{
return _myobject;
}

The Set method is:

Assign
-(void) Setmyobject: (ID) newvalue{
_myobject = newvalue;
}
Retain
-(void) Setmyobject: (ID) newvalue{
if (_myobject!= newvalue) {
[_myobject release];
_myobject = [newvalue retain];
}
}
Copy
-(void) Setmyobject: (ID) newvalue{
if (_myobject!= newvalue) {
[_myobject release];
_myobject = [newvalue copy];
}
}

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.