Class Declaration
Objective-C classes are declared using @interface block, and implementations are specified in @implementation block.
// ---- @interface ---- @interface Fraction : NSObject { // inherit from NSObject int numerator; int denominator; } - (void)print; - (void)setNumerator:(int)n; - (void)setDenominator:(int)d; @end // ---- @implementation ---- @implementation Fraction // you can optionally specify :NSObject after Fraction - (void)print { NSLog(@"%i/%i", numerator, denominator); } - (void)setNumerator:(int)n { numerator = n; } - (void)setDenominator:(int)d { denominator = d; } @end
Properties
@property is a shortcut for declaring getter and setter methods on your
class' instance variables. It is a feature only available in Objective-C 2.0. Not only are these
synthesized getters and setters atomic i.e. thread-safe, the setter
methods will also automatically release the overwritten objects.
More Resources
- Apple Doc the official documentation on properties.
- Cocoa with Love has more on the merit of property.
- Cocoacast a tutorial on property.
- Wikipeida Objective-C 2.0 Properties.