Skip to content

Instantly share code, notes, and snippets.

@chrisdevereux
Created August 28, 2011 18:20
Show Gist options
  • Save chrisdevereux/1177004 to your computer and use it in GitHub Desktop.
Save chrisdevereux/1177004 to your computer and use it in GitHub Desktop.
Using protocols to avoid method signature mismatches on class methods
#import <Cocoa/Cocoa.h>
@protocol IdMethod
+ (id) method;
@end
@interface IdClass : NSObject <IdMethod>
@end
@implementation IdClass
+ (id) method
{
return @"hello!!";
}
@end
@protocol StructMethod
+ (NSRect) method;
@end
@interface StructClass : NSObject <StructMethod>
@end
@implementation StructClass
+ (NSRect) method
{
return CGRectMake(5,5,5,5);
}
@end
int main (int argc, char const *argv[])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
Class<IdMethod> a = [IdClass class];
NSLog(@"%@", [a method]);
Class<StructMethod> b = [StructClass class];
NSLog(@"%@", NSStringFromRect([b method]));
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment