Split Obj-C classes into multiple files
I'd do it like this:
If you are putting the methods in a category then the declarations need to be in the category too, otherwise, as you have seen, the compiler will complain that the methods aren't implemented (because they're not in the main class implementation).
You could then, if you wish, have a further header file which just imports your class and category headers for convience.
Code:
//MyClass.h
@interface MyClass : NSObject
{
members;
}
@properties;
-(void) methods; //don't include category methods
@endCode:
// MyClass.m
#import "MyClass.h"
@implementation MyClass
//methods
@endCode:
// MyClass+Category.h
#import "MyClass.h"
@ implementation MyClass (Category)
// methods
@endCode:
// MyClass+Category.m
#import "MyClass+Category.h"
@implementation MyClass (Category)
// methods
@endIf you are putting the methods in a category then the declarations need to be in the category too, otherwise, as you have seen, the compiler will complain that the methods aren't implemented (because they're not in the main class implementation).
You could then, if you wish, have a further header file which just imports your class and category headers for convience.
Code:
//MoreConvenientHeader.h
#import "MyClass.h"
#import "MyClass+Category.h"
You should modify your main class file to include the category interfaces as well. Like so:
The rest of the code would be laid out the same.
Code:
// MyClass.h
@interface MyClass : NSObject
{
members;
}
@properties;
-(void) methods; // Don't include category methods...
@end
@interface MyClass (category)
-(void) methods; // Category methods in here.
@endThe rest of the code would be laid out the same.
What Blacktiger said.
BlackTiger,
Perfect! Thank you very much.
I don't understand why the documentation doesn't just explain it that way. It's what makes working in C-based languages such a pain in the ass.
Perfect! Thank you very much.
I don't understand why the documentation doesn't just explain it that way. It's what makes working in C-based languages such a pain in the ass.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Cocos2d enemy classes | NialG | 4 | 5,429 |
Dec 31, 2008 05:07 AM Last Post: NialG |
|

