Accessors

Accessors are methods used to query or change the internal state of an object. The internal state of objects is usually stored in instance variables. There is commonly a one-to-one correspondence between instance variables and accessor methods. The convention of using accessors is an extremely good practice that is ubiquitous in the Cocoa frameworks, but is optional.

The following simple class declaration shows how accessors are used.

@interface MYClass : NSObject 
{ 
  NSRect _myRect; 
} 

- (void)setRect:(NSRect)aRect; 
- (NSRect)rect; 
- (void)getRect:(NSRect *)aRectPtr; 

@end 

NSRect is a C structure defined in NSGeometry.h. The -setRect: method passed the aRect argument by value even though NSRect is a structure. Similarly, the -rect

Get Cocoa® Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.