Just a reference note on how to implement a “protocol” in Objective C.
First. We define our protocol in a file named “Protocol.h”:
@protocol Protocol @required - (void)requiredMethod:(NSString*)param; @optional - (void)optionalMethod; @end
We can declare a method as optional or required. If the method is required you must implement that method in your class.
Next, we decided to use the protocol in our class, first the header “UseProtocol.h” then the implementation “UseProtocol.m”:
#import <Foundation/Foundation.h> #import "Protocol.h" @interface UseProtocol : NSObject <Protocol> @end
#import "UseProtocol.h" @implementation UseProtocol - (void)requiredMethod:(NSString*)param { NSLog(@"%@", param); } @end
and next we glue it up together in a main.m
#import <Foundation/Foundation.h> #import "UseProtocol.h" int main(int argc, const char* argv[]) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; UseProtocol* p = [[UseProtocol alloc] init]; [p requiredMethod:@"test"]; [p release]; [pool drain]; return 0; }
We could use XCode but is too heavy for our little task…
If you used an editor, you can compiled it through command line:
clang -framework Foundation UseProtocol.m main.m -o main
Image may be NSFW.
Clik here to view.

Clik here to view.
