tags:

views:

53

answers:

2

Hello!

I have a problem with classes. This is my code:

self.shapeClass = [HWRectangle class];
if ([_shapeClass isKindOfClass:[HWRectangle class]]) {
    NSLog(@"Class created as: %s", [_shapeClass description]);
}

I thought that the program will do the logging in this case, but it doesn't. Do you have any idea why not?

+2  A: 

because: if ([_shapeClass isKindOfClass:[HWRectangle class]])

_shapeClass should be an instance of the class you are testing, unless you are really testing for class comparisons. So, this method is instance to class comparison, not class to class comparison.

For bonus points, your format string should be: NSLog(@"Class created as: %@", [_shapeClass description])

(unless you have overridden the NSObject method (which you should not do))

Justin
To wit, the method the asker was looking for is `isSubclassOfClass:` rather than `isKindOfClass:`.
Chuck
A: 

isKindOfClass checks the class of a variable. You say that shapeCalls = [HWRectangle class]. The result of [HWRectangle class] is of the class "Class". So if you compare this with the class of HWRectangle you will find that the two are not the same.

Vincent Osinga