Skip to content

Instantly share code, notes, and snippets.

@tedhagos
Created November 19, 2012 13:43
Show Gist options
  • Save tedhagos/4110713 to your computer and use it in GitHub Desktop.
Save tedhagos/4110713 to your computer and use it in GitHub Desktop.
Just messing around
/*
Protocols behave like interfaces in Java. You don't inherit implementation
but its a way to ensure that certain classes/objects do implement the
enforced methods
*/
#import <Foundation/Foundation.h>
@protocol Printing
-(void) print;
-(void) clean;
@end
@protocol Faxing
-(void) print;
-(void) fax;
@end
@interface Multi : NSObject <Printing, Faxing> {
}
@end
@implementation Multi
-(void) print {
}
-(void) fax {
}
-(void) clean {
}
@end
/////////////////////////////////////////////////
int main() {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Multi * multi = [[[Multi alloc] init ] autorelease];
[multi print];
[multi fax];
/*
Here, we declare the what variable as type id, but that it
needs to conform the Printing protocol. Well, the actual object
is a Multi type, which of course conforms to the protocol Printing
among other things.
*/
id <Printing> what = [[[Multi alloc] init] autorelease];
[what print];
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment