Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need assistance regarding Objective-c properties concept

I am reading Apple Doc for understanding property instance variable but bit confused

From Apple Doc:

Most Properties Are Backed by Instance Variables By default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler.

An instance variable is a variable that exists and holds its value for the life of the object. The memory used for instance variables is allocated when the object is first created (through alloc), and freed when the object is deallocated.

Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

Although it’s best practice for an object to access its own properties using accessor methods or dot syntax, it’s possible to access the instance variable directly from any of the instance methods in a class implementation. The underscore prefix makes it clear that you’re accessing an instance variable rather than, for example, a local variable:

If using accessor methods or dot syntax is best practice then why user _ivarPropertyName?

Why use ivar for presenting properties? what are its benefits? when apple says "using accessor methods or dot syntax is best practice"

like image 851
S.J Avatar asked Mar 19 '13 07:03

S.J


People also ask

What are Objective-C properties?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.

Do people still use Objective-C?

Furthermore, Objective-C is a piece of art, creators packed genius solutions and were constantly improving it, so us developers were able to use it at our advantage. There are a lot of indicators telling us there's still a ton of legacy Objective-C code, both from Apple and from other developers, that's still in use.

Is Objective-C proprietary?

And, since then, Objective-C has been the primary language for software at Apple. It's important to note here that Objective-C is a proprietary language, which means that only Apple can make core changes to the language.


1 Answers

@property declares the existence of a property (describing its interface), but doesn't specify the implementation of that property. But properties need to store their contents somewhere. By default, the compiler synthesizes an ivar for that (and matching setters and getters). So normally you can ignore the existence of the ivar and just use dot syntax.

I follow Apple's advice and try to avoid using ivars directly. But somtimes you want to access a property without invoking its getter. The most common exception in my code is lazily-initialized read-only properties:

@interface MyObject : NSObject
@property ( nonatomic, readonly ) id someProperty ;
@end

@implementation MyObject
@synthesize someProperty = _someProperty ; // required; compiler will not auto-synthesize ivars for readonly properties

-(id)someProperty
{
    if ( !_someProperty )
    {
        _someProperty = ... create property here
    }

    return _someProperty ;
}

@end

Also, you may not want to invoke the getter for a property in your -dealloc method... for example, a timer property. To avoid creating a timer in -dealloc, access the ivar directly:

-(void)dealloc
{
    [ _myTimer invalidate ] ; // don't use self.myTimer here, that would create a timer even though we're going away...
}

There are probably more use cases. For most properties you don't even need to use the ivar, just use <value> = self.property and self.property = <new value>.

edit:

Also, there will be some additional overhead for accessing the property via message dispatch (using dot-accessor syntax or the getter) vs directly accessing the ivar, but it will make no difference in almost all cases.

like image 114
nielsbot Avatar answered Sep 30 '22 05:09

nielsbot