I have a class (MyClass
) with a lot of methods. Consequently, the .m file has become quite difficult to read. I'm relatively new to Objective-C (having come from REALbasic) and I was wondering if it's possible to put some of the methods in MyClass
into different files and then include them in the class. How would I go about this in Xcode?

- 8,096
- 22
- 65
- 103
3 Answers
Yes it is possible and fortunately this can be done easily in Objective-C with Categories.
Say you have your base class MyClass
.
@interface MyClass : NSObject
-(void) methodA;
@end
And the according implementation file (not relevant here).
Then you can create a category by defining a new interface in a new header file:
// the category name is in parenthesis, can be anything but must be unique
@interface MyClass (extended)
-(void) methodB;
@end
and the implementation file:
@implementation MyClass (extended)
-(void) methodB {
}
@end
Common convention to name these files is ClassToAddMethodsTo+CatgoryName, i.e.:
MyClass+extended.h
MyClass+extended.m
Group related functionality into categories and give it a meaningful name.

- 795,719
- 175
- 1,089
- 1,143
In Objective-c you can break a class into 'categories' - a class spread across many files. The normal Object-Oriented way is to use SuperClasses and SubClasses.
This is almost certainly a code smell telling you that you have a design problem. See this antipattern

- 9,128
- 5
- 37
- 58
-
Using categories is not *certainly* code smell. E.g. if you want to extend a class for which you do not have the source code. Or if you just want to structure your class in logical blocks. But your right so far, as it is probably code smell if your class has really a lot methods (*god like class*). – Felix Kling May 25 '10 at 11:52
-
Hey Felix ***!@ - Having a class so large you can't manage it is the code smell. – hooleyhoop May 25 '10 at 12:16
-
@mustISignUp: Definitely, and I said your are right in this point. But your answer can also be interpreted that using categories *in general* is code smell. I just wanted to emphasize that it is not. – Felix Kling May 25 '10 at 12:21
-
You're right. Categories themselves are very useful for grouping functionality or even extending classes, amongst other things. – hooleyhoop May 25 '10 at 12:24
-
I see your argument regarding a God-like class. I am bearing this in mind. Having now split my class into separate files using categories though it certainly seems to make a lot more logical sense. – Garry Pettet May 25 '10 at 14:42
-
You might find this interesting http://en.wikipedia.org/wiki/Single_responsibility_principle – hooleyhoop May 25 '10 at 15:01
-
This is absurd! Sometimes classes are simply big. Write a recursive descent parser for any reasonably sized language and see how small a class you get. Class extensions can separate portions of your code and actually make it easier to read by separating concerns. The OOP paradigm implies hierarchy, but if there are no such relations among the methods in your class, then creating superclasses where inheritance is meaningless is where the real bad smell is. Context is everything, not pure length. – Luis Vieira Damiani Oct 14 '17 at 04:27
There is one thing you could do..........
But be warned, some might consider this pure blasphemy. :)
Say you have a class with two methods you want to have in separate files. You'll have three files:
• Class.h
• Class.m
• Class_otherMethod.m
Your Class.h should look just like any other. I think it's better to keep the header file complete, but this 'trick' can work on separating .h files just as well.
@interface Class : NSObject
- (void) method;
- (void) otherMethod;
@end
In your Class.m file you will #include
the Class_otherMethod.m inside the Class @implementation
like this:
#import "Class.h"
@implementation Class
- (void) method {
// do something.
}
#include "Class_otherMethod.m"
@end
And your Class_otherMethod.m file will have only the bare otherMethod implementation:
- (void) otherMethod {
// do something different.
}
Why this works
It's quite simple actually. The preprocessor simply "pastes" the content of Class_otherMethod.m inside the Class.m file and the compiler treats it as one big long file. :P

- 1,924
- 2
- 18
- 26
-
And [this guy](http://stackoverflow.com/a/15315895/330914) took it a whole lot further... :P – Alex Zak Oct 11 '13 at 12:34