// -*- objc -*- // @@PLEAC@@_NAME Objective-C // @@PLEAC@@_WEB http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html // @@PLEAC@@_INTRO // @@SKIP@@ This is Objective-C in the context of Apple use - using NextStep foundation classes; // @@SKIP@@ e.g. under Linux, GNUStep runtime and development stuff are needed. A program can // @@SKIP@@ then be successfully compiled using the following commandline: // @@SKIP@@ gcc -x objective-c -Wno-import [source files...] -lgnustep-base -fconstant-string-class=NSConstantString. // @@PLEAC@@_APPENDIX @implementation test // @@PLEAC@@_1.0 // Objective-C supersedes traditional C char* strings with constant // string objects, using the @ character before the opening ". NSString *string1 = @"Jon 'Maddog' Orwant'"; NSString *string2 = @"\n"; // a "newline" character NSString *string3 = @"Jon \"Maddog\" Orwant"; // literal double quotes // @@PLEAC@@_13.0 // A class has a header part traditionally named HumanCannibal.h in that case... #import <Foundation/Foundation.h> @interface HumanCannibal : NSObject { // instance variables NSString *stomach; NSString *place; } - (void) feed: (NSString*) input; - (void) move: (NSString*) input; + (void) classOnlyMethod; @end // ...and an implementation part named HumanCannibal.m in that case #import "HumanCannibal.h" @implementation HumanCannibal - (void) init { // this is the constructor. do whatever but don't forget to // call the parent constructor! [super init]; } - (void) feed: (NSString*) input { stomach = input; } - (void) move: (NSString*) input { place = input; } - (NSString*) description { return [NSString stringWithFormat: @"In %@, stomach contains %@", place, stomach]; } + (void) classOnlyMethod { // do whatever } @end // method calls are between square brackets #import "HumanCannibal.h" int main(void) { HumanCannibal *lector = [HumanCannibal new]; NSLog(@"%@", lector); [lector feed: @"Zak"]; [lector move: @"New York"]; NSLog(@"%@", lector); } // # gcc -x objective-c -Wno-import HumanCannibal.m main.m -lobjc -lgnustep-base -fconstant-string-class=NSConstantString && ./a.out // 2010-07-02 13:19:47.954 a.out[8288] In (nil), stomach contains (nil) // 2010-07-02 13:19:47.958 a.out[8288] In New York, stomach contains Zak