S.J
S.J

Reputation: 3071

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"

Upvotes: 6

Views: 1019

Answers (1)

nielsbot
nielsbot

Reputation: 16031

@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.

Upvotes: 7

Related Questions