Question

Up until recently, I had a project compiling fine in XCode; however now I am scratching my head with the following:

Cannot define category for undefined class 'myClass'

I have a header that extends another through a category, like such:

myClass.h

@interface myClass:UIView<...>
@property ....
-(void) method....
@end

myClass+myCategory.h

#import "myClass.h"
@interface myClass (MyCategory)
-(void) method2...
@end

myClass.m

#import "myClass.h"
#import "myClass+myCategory.h"
...

My understanding from searching, as well as from the history of this project compiling and running is that this should be valid Objective-C, which leads me to XCode. Any ideas here?

Was it helpful?

Solution

The problem here is that your compiler is running into the category header before it's seen the file that the category is defined or referenced in.

D Carney was on to something when he said that adding the import to the precompiled header would help, but he's mistaken in what to import. You want to ensure that your compiler sees your base class header before the category so it knows what class the category applies to.

OTHER TIPS

Well, I have no explanation for the above issue. I created a new project and re-added all my source files to it. Compiles now without any issue.

I ran into the same issue. Turns out that you need to add the category include to your ...-Prefix.pch file:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
    #import "myClass+myCategory.h"
#endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top