user523234
user523234

Reputation: 14834

Class resulted from NSClassFromString does not associate with class defined in header file

In the following code, I used NSClassFromString to convert say an NSString "ColorFilter" to class name ColorFilter. The result ColorFilter class does not seem to associate with the class defined in my header file in the import statement with the same name.

in my .h file:

#import "ColorFilter.h"   //ColorFilter is a subclass of UIViewController which also has an iVars name imageView of UIImageView class

in my .m file:

NSString *filter = @"ColorFilter";
UIViewController *myFilterClass = [[NSClassFromString(filter) alloc] initWithNibName:filter bundle:nil];

The error I got from the line above was [ColorFilter initWithNibName:bundle:]: unrecognized selector sent to instance...

For testing, I modified the last line to be:

UIViewController *myFilterClass = [[NSClassFromString(filter) alloc] init];
myFilterClass.imageView = .....;

Now the error I got was -[ColorFilter setImageView:]: unrecognized selector sent to instance...

My conclusion is that the ColorFilter class that was produced by NSClassFromString does not know anything about this #import "ColorFilter.h".

Any idea what my mistake was?

Upvotes: 0

Views: 770

Answers (2)

user523234
user523234

Reputation: 14834

I discovered that one of my static library already has the "ColorFilter" class defined.

It looked like at compile-time, one can overwrite a existing class in the static library. But at run-time, the classes in static library have precedence over dynamically generated classes.

Upvotes: 0

Alex Zielenski
Alex Zielenski

Reputation: 3601

ColorFilter.m might not be compiled with your app. In the build sources tab of your project, under compile sources, do you see "ColorFilter.m"? If not, drag it into that list.

Upvotes: 2

Related Questions