Split Obj-C classes into multiple files

Member
Posts: 245
Joined: 2005.11
Post: #16
I'd do it like this:
Code:
//MyClass.h

@interface MyClass : NSObject
{    
  members;
}

@properties;

-(void) methods;  //don't include category methods

@end

Code:
// MyClass.m

#import "MyClass.h"

@implementation MyClass

//methods

@end

Code:
// MyClass+Category.h

#import "MyClass.h"

@ implementation MyClass (Category)

// methods

@end

Code:
// MyClass+Category.m

#import "MyClass+Category.h"

@implementation MyClass (Category)

// methods

@end

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:
//MoreConvenientHeader.h

#import "MyClass.h"
#import "MyClass+Category.h"
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #17
You should modify your main class file to include the category interfaces as well. Like so:

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.

@end


The rest of the code would be laid out the same.
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #18
What Blacktiger said.
Quote this message in a reply
Member
Posts: 73
Joined: 2009.03
Post: #19
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cocos2d enemy classes NialG 4 6,285 Dec 31, 2008 05:07 AM
Last Post: NialG