1

would you please break my confusion.

If I define a property in a class

@interface Class
{
  UIScrollView * _scrollView;
}
@property (nonatomic, retain) IBOutlet UIScrollView * scrollView;
@end

@implement
@synthesize scrollView = _scrollView;
@end

When I wanna implement it, I can use

_scrollView.contentSize = xxx

or

self.scrollView.contentSize = xxx

What's the difference between the two description?


Thanks for your answering...

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Hot Licks' answer is correct. You might also be interested in this answer to help clarify things. http://stackoverflow.com/a/8032148/470879 – MattyG Sep 27 '12 at 03:09

1 Answers1

2

The direct reference to the instance variable is precisely that -- a reference to a field in the instance, unaffected by the fact that it's also the "backing store" of a property.

The self.propName reference, on the other hand, is shorthand for either [self propName] (if reading) or [self setPropName:newPropValue] (if setting). Ie, those references go through accessor methods. This isn't real important if the property is defined as assign, but if it's retain then the setter method takes care of all the retain logic.

Further, you can implement your own property accessors -- -(SomeType*) propName {...} and -(void) setPropName:(SomeType*)propParm {...} -- if you want to have them do something special, such as "lazy" initialization.

(Also, properties default to "public" access, while instance variables default to "private" access.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151