Question

I am extending a class from a external library. Here is my code:

Header file: Manager+MyCategory.h

#import "Manager.h"
#import "Element.h"

@interface Manager (myCategory) 
- (Element*) elementWithTag:(NSInteger)tag; 
@end

Implementation file: Manager+MyCategory.h file

@implementation Manager (myCategory) 

- (Element*) elementWithTag:(NSInteger)tag {
    ...
}

@end

Now here is how I use this category:

#import "Manager+MyCategory.h"

@implementation myClass 

- (void) myFunction:(NSInteger)tag {
   Manager* myManager = [[Manager alloc] init];
   Element* = [myManager elementWithTag:tag];

   ...

   [myManager release];
}

@end

When I compile I have no warnings but when I run my program I get the following error:

*** -[Manager elementWithTag:]: unrecognized selector sent to instance 0x105d9a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Manager elementWithTag:]:

Header file (ie Manager+MyCategory.h) is correctly imported but it looks like the link with the implementation file is not done. If I replace #import "Manager+MyCategory**.h**" by #import "Manager+MyCategory**.m**" in my class it's working properly.

Anyone can explain me what I forgot? Thanks

Was it helpful?

Solution

Categories do not work well with traditional Unix static libraries.

If that's your case, it seems that passing the -ObjC option to the linker resolves the issue.

Apple Technical Q&A QA1490 explains it all.

OTHER TIPS

I suspect that you have forgotten to add the Manager+MyCategory.m to the approrpriate target in Xcode. You do not get a linker error because all the symbols exist and there is no compiler warning because you have included the appropriate header files, but at runtime the implementation of the elementWithTag: method is missing.

Apologies, could not add this as a comment...

Just for testing, you may want to add an extern c function declared in the header and implemented in the .m file. If you call this c function within your test code then this will generate a linker error due to an unresolved identifier.

This can make it easier to investigate the problem as it provides immediate feedback and may generate some additional clues as to why the file is not being included.

Given the time frame, I'll assume you've done this, but just in case... make sure that you've cleaned the target, shut down XCode, and then re-open. I've had the project files become corrupted in memory and act strangely.

Barney

I had this issue when upgrading to Xcode 4.

To fix I had to separate my category definition into a separate .m and a .h - Xcode 3 seemed okay with the implementation and interface being in a .h ... but oh no fussy Xcode 4 is rather anal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top