Can someone explain why isKindOfClass returns different results depending on how the instance was created?
@interface BaseClass ... @interface DerivedClassA : BaseClass ... DerivedClassA *instance = [[DerivedClassA alloc] init]; [instance isKindOfClass:[BaseClass class]]; // yields YES Class c = NSClassFromString(@"DerivedClassA"); id instance = [[c alloc] init]; [instance isKindOfClass:[BaseClass class]]; // yields NO
Everything that I can extract from the two types in the debugger is identical. I can even compare both results of NSStringFromClass ([class superclass]) and they are equal.
I need to miss something simple.
Updated Code
This is the unit test code.
LightingUnit *u1 = [[LightingUnit alloc] init]; STAssertTrue([u1 isKindOfClass:[ModelBase class]], @"should be derived from base"); Class uc = NSClassFromString(@"LightingUnit"); id u2 = [[uc alloc] init]; STAssertTrue([u2 isKindOfClass:[ModelBase class]], @"should be derived from base");
Here are the class definitions.
@interface ModelBase : NSObject @property (readonly) NSString *__type; - (id)initWithDictionary:(NSDictionary *)dictionary; - (NSMutableDictionary *)dictionary; @end @interface LightingUnit : ModelBase @property (strong, nonatomic) NSString *name; @property NSInteger unitId; @end
Possible answer
When I run this logic outside the test environment, it works without problems. Obviously, the only difference is removing the STAssertTrue statements and replacing them with my conditions. In this case, they both return YES. I even tried to create a simplified example (no ivars in the database or derivative), and it fails in testing, but it works in the standard runtime.
Any ideas why this might be a problem while testing? Is my test target missing?
solvable
I have included .m files in compilation targets for testing purposes. After removal, it began to behave as expected. Thanks to this post for helping me resolve this issue.
Brian mahloch
source share