Objective C: Anatomy of a Class
In object-oriented languages, methods that get and set instance variables are called accessors. Individually, they are respectively called getters and setters.
Here’s an example of an Objective-C class showing how instance variables are declared and how accessors are declared.
#import <Foundation/Foundation.h>
@interface Possession : NSObject
{
// Instance variables are declared in curly brackets (accolades)
NSString *possessionName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;
}
// Using the '@properties' keyword, we declare the getter and setter in one line
@property (nonatomic, copy) NSString *possessionName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly) NSDate *dateCreated;
@end
However, that is not all there is to implement accessor methods. We have to synthesize the accessor methods in our implementation (.m) file, like this:
@implementation Possession @synthesize possessionName, serialNumber, valueInDollars, dateCreated; @end
Now that our properties are synthesized, we can send messages to Possession instances to get and to set instance variables.
In other words, we can send the messages valueInDollars and setValueInDollars: to instances of Possession!
Pingback: iOS dev launcher: Session #1 SaveThePrincess - Calvin's