Difference between property of class mVar and instance variable self.mVar

I'm a little confused by the difference between accessing an instance variable via self or just by name (when working inside a class).

For example, take this class:

//In .h file: @interface Register : NSObject { NSString *mName; } - (id) initWithName:(NSString *) name; //In .m file: - (id) initWithName:(NSString *)name { if (self == [super init]) { mName = name; } return self; } 

What is the difference between accessing an instance variable using

 self.mName = name; 

against

 mName = name; 

Which is not @property and is not @ sythenize'd.

Let's say it is, although in this example:

 //In .h file: @interface Manange_My_ViewsViewController : UIViewController { IBOutlet UILabel *countLabel; } @property (nonatomic, retain) IBOutlet UILabel *countLabel; //In .m file: @synthesize countLabel; - (void) updateLabel:(NSUInteger)count { countLabel.text = [NSString stringWithFormat:@"%d", count]; } 

But I will say that I turned to countLabel as:

 self.countLabel 

What's the difference?

Edit: third example for each user: Let's say iVar was not an IBOutlet:

 //In .h file: @interface Fake : NSObject { NSString *mVar; } @property (nonatomic, retain) NSString *mVar; //In .m file: @synthesize mVar; mVar = @""; 

VS

  self.mVar = @""; 

Or is it the same thing: in the first we get access to the actual instance variable, and in the second we actually go through the automatically created setter (via @synthesize)?

Thanks everyone!

Edit: update in response Peter Hosey ...

So, do you think the mVarName convention is bad? I took this from my C ++ days.

But what about when you do?

 -(void) someMethod:(int) x { x = x; } 

You cannot do this (say, "x" is also a class variable)

But you can do:

 -(void) someMethod:(int) x { mX = x; } 

But your statement is better to do:

 -(void) someMethod:(int) x { self.x = x; } 
+4
source share
3 answers

What is the difference between accessing an instance variable using

 self.mName = name; 

against

 mName = name; 

The first is property access syntax. It is converted to an object access message (in this case, self ). That is, this statement implicitly translates this message expression expression:

 [self setMName:name]; 

(Incorrect accessory names, such as why โ€œmNameโ€ is a bad name for a property. The property declaration syntax is used to do this, letting you name the property name โ€œnameโ€ and your instance variable โ€œmNameโ€ and map one to the other.)

The second example directly accesses the message of the instance variable without access.

Which is not @property and is not @ sythenize'd.

Say it's all the same ...

If a property with the name " mName " is not declared for the class, you cannot use the property access syntax to access the property using this name in an instance of this class.

And it doesnโ€™t matter if you synthesize accessors, manually translate them into a superclass with @dynamic or define them yourself. The way the object will respond to the access message, but the access message that the compiler generates, will not differ (since access to properties can just as easily happen from outside the class as from the inside).

Let's say iVar was not an IBOutlet:

It does not matter. IBOutlet means just anything for IB. Everything else is all the same.

In fact, IBOutlet is currently just a macro that expands to zero. After your code receives preprocessing, the word "IBOutlet" no longer exists, so the compiler never sees it. This is slightly different from anything other than IB: Nothing.

Edit in response to editing a question

I said that mName is bad as a property name, due to the access names that follow from it. The instance variable name is a separate issue, especially because the property and ivar must not have the same name.

For a variable, whether it be an instance variable or a local variable, choosing name or m_name or mName is purely a style choice.

someMethod: usually an accessory, setX: . Inside this method, self.x = x , which is [self setX:x] , causes infinite recursion. So do not do this.

When someMethod: not an accessory (either init or dealloc ), using a property is just fine and usually preferable. However, in this case, you are unlikely to pass one of your arguments the same name as the instance variable. When such a case may occur, specify the local variable more specifically, since its purpose is more specific. This is also a style issue.

When it is an accessor, I find the local variable newX by naming the instance variable the same as the property, x . This is my personal style; as I said, naming the property x , ivar mX , and the local variable x is fine too (except for the excessive brevity of this example).

+7
source

OK, the main difference is disabled first:

 mVar = var; 

It is simply a change in meaning. What is it.

 self.mVar = var; 

This is equivalent to:

 [self setMVar:var]; 

In other words, one calls the method, the other does not. Using the @property syntax can give you some really useful benefits. For example, you get free keyword coding. This means that another object can observe this property of the mVar object and is automatically notified of every change, without any action. You will not get this if you simply access ivar directly. (Unless, of course, you implement it yourself. But why are you doing this?)

You also get semi-free memory management. If you declare a property as (retain) , you do not need [newValue retain] yourself. The synthesized method will do this for you (in both cases, you still have to [ivar release] in your dealloc method).

You can also get some degree of thread safety. If you do not declare a property as (nonatomic) , then it is (by default) atomic (although this keyword does not exist, it is implied). This means that reading / updating the value of the atomic operation property. If you just need to access ivar directly, you will have to implement atomicity yourself using locks.

In principle, using synthesized methods, you get some useful things for free. The only reason I would say not to use @property syntax is if you have conclusive evidence that calling these methods is a bottleneck in your code. However, it will be very difficult for you to find a situation where this is the case.

+3
source

First of all, with a read-only condition, which is essentially an IBOutlet, that doesn't matter.

The key difference is that the property actually calls the accessor method when the instance variable is accessed directly.

Thus, to set the save property, using self and accessor will free the old object and save the new one. Setting an instance variable does NOT directly affect the number of stored objects of any objects.

Using @synthesize will create standard accessories for you.

The main reason for using properties is that since they are accessories, they can be read and / or modified outside the class.

+1
source

Source: https://habr.com/ru/post/1301531/


All Articles