0

Possible Duplicate:
iVar property, access via self?

I understand that it's to use setters for instance variables, but are ther any reasons to use getters istead of direct access?

.
.
NSArray *instArray;
.
.
@property (nonatomic, retain)NSArray *instArray;
.
.
@synthesize instArray;
.
.
//are the following lines equal?
NSArray *array = [NSArray arrayWithArray:self.instArray];

NSArray *array = [NSArray arrayWithArray:instArray];
Community
  • 1
  • 1
Michael
  • 1,238
  • 2
  • 15
  • 26
  • 1
    See also: http://stackoverflow.com/questions/2914399/does-it-make-a-difference-in-performance-if-i-use-self-foobar-instead-of-foobar http://stackoverflow.com/questions/4271657/when-to-access-properties-with-self http://stackoverflow.com/questions/3494157/in-objective-c-on-ios-what-is-the-style-difference-between-self-foo-and-foo http://stackoverflow.com/questions/3318499/ivars-with-and-without-self – jscs Sep 14 '11 at 18:21
  • Thank you for above links they are all useful for me. – Michael Sep 15 '11 at 10:55
  • Glad that you found them helpful! – jscs Sep 16 '11 at 06:39

1 Answers1

3

For purely synthesized getters, there is no difference, unless you want atomic access (atomic properties surround the code with a lock).

For properties that have getters with extra code, there is a difference. Sometimes, it makes sense to avoid the extra code, so you use the ivar (say, xyz) directly. Otherwise,if you want the extra code to be invoked, you use the property, using self.xyz.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94