Objective-C: Instance Variables
Wiki Index
Reference: Encapsulating Data
- Use properties (accessible from outside the class in question):
@interface XYZPerson : NSObject @property NSString *firstName; @property NSString *lastName; @end
- These
@property
declarations also synthesize instance variables, so you can do this:- (void)someMethod { _firstName = @"Bob"; }
- Use
@synthesize
to control the name of the generated instance variable:@implementation XYZPerson @synthesize firstName = ivar_firstName; @end
- These
- Or define instance variables directly:
@implementation SomeClass { NSString *_anotherCustomInstanceVariable; } ... @end
- Block scoping is relevant here; the following is incorrect and declares global variables instead (source):
@implementation SomeClass NSString *_anotherCustomInstanceVariable; ... @end
- Block scoping is relevant here; the following is incorrect and declares global variables instead (source):