Objective-C if ([self isKindOfClass: ...]) static method

I'm looking for help in understanding why the code is behaving so strangely. What do I have: BaseClass : NSManagedObject

andChildClass : BaseClass

BaseClass has a category..h:

@interface BaseClass (Category)

+ (NSArray)method;

@end

      

.m:

@implementation BaseClass (Category)

+ (NSArray *)method
{
    if ([self isKindOfClass:[ChildClass class]) {
        do stuff
        return resultArray;
    }
    return nil;
}

      

Elsewhere in the project, I call *array = [ChildClass method];

. In the console, the implementation of BaseClass (Category) is readable self = (Class)ChildClass

, but still the execution flow skips if

and for whatever reason jumps to return nil;

, which is beyond my understanding. Any suggestions why this might be? All answers are appreciated. Thank.

... since I don't have enough reputation points, I don't post screenshots. I hope I get it.

+3


source to share


2 answers


You have a static method, so it represents the class itself. It is enough to do this:



if (self==[ChildClass class]) {
    do stuff
    return resultArray;
}

      

+4


source


You might be better off doing [[childOrBaseInstance class] method];

in your code that calls +method

.

ChildClass

would have its own +method

that overrides BaseClass

+method

.



Whenever you inspect your class and do something different, ask yourself if you are just ruminating polymorphism.

+1


source







All Articles