Objective-c – Can categories be created only on Objective-C classes

objective c

All of the examples of categories that I've seen use some Objective-C class rather than a custom class. For example: NSMutableArray, NSArray, NSString. I'd like to create a category for a custom class. Here's a useless example I created just to test but fails compilation:

//person.h
@interface Person (aCategory) : NSObject {
NSString *MyName;
NSString *webAddress;
}
@property(nonatomic, retain) NSString *MyName;
@end

//person.m
#import "Person+aCategory.h"

@implementation Person
@synthesize MyName;
@end

And the category definition:

@interface aCategory : NSObject {
NSString *webAddress;
}
@property(nonatomic, retain) NSString *webAddress;
- (void)changeWebAddress;
@end

//in aCategory.h
#import "aCategory.h"

@implementation aCategory
@synthesize webAddress;

- (void) changeWebAddress{
self.webAddress = @"http://www.abc.com";
}
@end

This will give the following errors and warning:

error: Person+aCategory.h: no such file or directory
warning: cannot find interface declaration for Person
error: no declaration of property 'MyName' found in interface

Is there a way to do this?

Best Answer

A custom Objective-C class you define is still an Objective-C class. You can create categories on them, yes. The problem here is that what you wrote isn't remotely the right way to define a category.

First you define a plain class. You put all your instance variables and whatever methods you want in there.

Then you define a category, which adds methods to the class it's on. Categories do not descend from any parent class or have ivars, because they just add methods to an existing class.

See Apple's The Objective-C Programming Language for full documentation.