objective-cperformancepropertiesivars

Using ivars to define property values inside a class


Is accessing the private ivar linked to a property inside of a class method more efficient than using its synthesized getter/setter methods, or is the efficiency just the same? ...As in:


@implementation MyApp

@synthesize name;

- (void)loadView 
{
    _name = @"Savagewood"; // VS.
    self.name = @"Savagewood";
}

@end

I'm guessing the latter takes more time to execute but I want to know what they suggest App developers to use for the sake of consistency and good programming technique and whether both assignments are basically of the same time complexity.


Solution

  • In general, it's best to use property accessors wherever possible, and limit the direct use of instance variables to accessor methods, init methods, and dealloc (if you're not using ARC). Conversely, avoid calling accessors in init and dealloc, and avoid using the accessors of the property you're implementing from within it's own accessor methods.