0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
rolling_codes
  • 15,174
  • 22
  • 76
  • 112
  • This is not a duplicate question. The answer may be the same but it's a completely different question – rolling_codes Jul 09 '14 at 22:52
  • See also: [Dot notation vs message notation for declared properties?](http://stackoverflow.com/q/1249392) – jscs Jul 09 '14 at 22:54
  • hmmm.. possibly but it doesn't directly talk about ivars the way i do – rolling_codes Jul 09 '14 at 22:56
  • There's no end of copies or variations of this question, most with high-quality answers that contain the information you've selected as your accepted answer. There's absolutely no reason for another open instance to exist. http://stackoverflow.com/q/2278389 http://stackoverflow.com/q/8030425 http://stackoverflow.com/q/536388 http://stackoverflow.com/q/6414502/ [&c.](http://stackoverflow.com/search?q=%5Bobjc%5D+self.ivar) – jscs Jul 09 '14 at 23:07
  • understandable. mind deleting my question for me then? – rolling_codes Jul 09 '14 at 23:29
  • I don't have the ability to do that singlehandedly; only a diamond mod does. I can just vote on it after a few days. – jscs Jul 09 '14 at 23:39
  • : ( well please don't downvote me. Can you ask an admin you know to delete it for me? – rolling_codes Jul 10 '14 at 00:30

2 Answers2

2

The latter would actually call the setter method on the property name. If you override the setter into something like

- (void)setName:(NSString*)name {
  NSLog(@"New name: %@", name);
  _name = name;
}

You'll see that setting ivar directly does not log anything, but the latter would trigger a log.

In terms of cost: I would say setting the ivar is cheaper, but the cost you save is almost negligible. My approach is use property only when necessary, like when you need KVO on a property of an object. Otherwise I always use an ivar.

Enzo
  • 969
  • 1
  • 8
  • 23
  • Yes this I know, but assuming that you don't customize the setter method? – rolling_codes Jul 09 '14 at 21:39
  • 1
    I would say setting the ivar is cheaper, but the cost you save is almost negligible. My approach is use property only when necessary, like when you need KVO on a property of an object. Otherwise I always use an ivar. – Enzo Jul 09 '14 at 21:41
  • do you mind posting that as answer? Do you know if there exists a standard way of accessing property values for developers? – rolling_codes Jul 09 '14 at 21:42
  • Sure. I'm not sure what do you mean by standard way? Do you mean a common programming pattern used in accessing properties? – Enzo Jul 09 '14 at 21:43
  • Yes that is considered the norm by iOS programmers. Like good technique – rolling_codes Jul 09 '14 at 21:43
  • umm... I guess the most important thing is just to put the right attributes on them? Like when something is readonly you want to make sure it's read only, if you don't want anyone to alter your internal state when they do stuff like `NSString *name = yourObj.name; name = @"new name"`, make sure your name property has `copy` attribute on it. – Enzo Jul 09 '14 at 21:46
  • 1
    You should use property accessors whenever possible, and limit use of ivars to accessor methods, init methods, and dealloc (if you're not using ARC). – jlehr Jul 09 '14 at 21:47
  • @jlehr that is actually more like the answer I was looking for. Mind making a separate answer? – rolling_codes Jul 09 '14 at 21:47
2

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.

jlehr
  • 15,557
  • 5
  • 43
  • 45
  • so how do i set variables in the init methods? using the ivars? – rolling_codes Jul 09 '14 at 21:56
  • 1
    @Savagewood Absolutely. Directly set ivars in init methods rather than calling setter methods to avoid side effects (such as KVO notifications). There's also the potential that a setter method might be coded to do something that depends on the value of another ivar, leading to order decency issues. – jlehr Jul 09 '14 at 22:09