////////////////////////////// //ex1 demonstrates : // //@interface // //@implementation // //class methods and object methods // //int main(void) the entry point for the program // //Sending messages //(in objective-c you don't "call a method", rather you send a message. Also note that messages aren't bound to a method implementation until runtime.) ////////////////////////////// #include <Foundation/Foundation.h> @interface MyClass: NSObject -(void) MyObjectMethod; //note the "-". This method is accessible at the object (instance) level +(void) MyClassMethod; //note the "+". This method is accessible at the class level (think static - see static variables in ex_) @end @implementation MyClass //@interface and @implementation are often in separate files -(void) MyObjectMethod { printf("object_method\n\r"); } +(void) MyClassMethod; { printf("class_method\n\r"); } @end int main(void) { // Sending a message is the way in which you pass instructions to an object causing it to respond and or take an action (change state, promt the user, etc.). // To send a message to an object, use the syntax: // // [receiver messagename]; // // where receiver is the object and messagename is the name of the message (i.e. the method name). // // Messages can include arguments that are prefixed by colons, in which case the colons are part of the message name. // Labels describing arguments can precede the colons. For example: // // [receiver setVar1: 111 Var2: 222]; // // this will invoke the method named setVar1:Var2: (in otherwords the entire name of the method is "setVar1:Var2:" including the colons.) [MyClass MyClassMethod]; //this is sending a message to a Class (this is like calling a static method in other languages) MyClass *myInstance = [MyClass new]; [myInstance MyObjectMethod]; // here we send the "MyObjectMethod" message to an instance of MyClass //////////////////////////////////////// //more below, demos alloc init //////////////////////////////////////// MyClass *my2ndInstance = [MyClass alloc]; [my2ndInstance init]; [my2ndInstance MyObjectMethod]; MyClass *my3ndInstance = [[MyClass alloc] init]; //nested "message sending" [my3ndInstance MyObjectMethod]; return 0; } |