Skip to content

Instantly share code, notes, and snippets.

@rgoulter
Created February 15, 2014 05:29
Show Gist options
  • Save rgoulter/9014947 to your computer and use it in GitHub Desktop.
Save rgoulter/9014947 to your computer and use it in GitHub Desktop.
ObjC Using Categories to Modularise Long File
#import "foo.h"
@interface Foo (foobar)
- (void) foo;
- (void) bar;
@end
#import "foo.h"
@interface Foo (spameggs)
- (void) spam;
- (void) eggs;
@end
#import <stdio.h>
#import <Foundation/Foundation.h>
@interface Foo : NSObject
- (void)foo;
- (void)bar;
- (void)spam;
- (void)eggs;
@end
#import "foo.h"
@implementation Foo
- (void) foo {
printf("foo\n");
}
- (void) bar {
printf("bar\n");
}
- (void) spam {
printf("spam\n");
}
- (void) eggs {
printf("eggs\n");
}
@end
#import <stdio.h>
#import "foo.h"
int main(int argc, char **argv) {
Foo *f = [Foo new];
[f foo];
[f bar];
[f spam];
[f eggs];
return 0;
}
#import "foo.h"
@implementation Foo (foobar)
- (void) foo {
printf("foo\n");
}
- (void) bar {
printf("bar\n");
}
@end
#import "foo.h"
@implementation Foo (spameggs)
- (void) spam {
printf("spam\n");
}
- (void) eggs {
printf("eggs\n");
}
@end
#import "foo.h"
#import "foo+foobar.h"
#import "foo+spameggs.h"
@implementation Foo
@end
@rgoulter
Copy link
Author

To compile with the "long" file, of course:

gcc -lobjc longfoo.m main.m -o longfoo.out

To compile with the "short" files:

gcc -lobjc shortfoo.m shortfoo+foobar.m shortfoo+spameggs.m main.m -o shortfoo.out

With the latter, however, since shortfoo.m is an incomplete implementation (we leave methods to categories), and because some methods of the primary class are implemented by categories, we get two warnings for each method: incomplete-implementation and objc-protocol-method-implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment