0

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
  • Don't start your ivar names with a single leading underscore. That's an Apple internal coding convention, and it exposes you to a hazard of name collision. Commented Nov 30, 2009 at 14:56
  • @NSResponder That only applies to method names, not instance variables... Commented Nov 30, 2009 at 17:19

2 Answers 2

3

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))

Sign up to request clarification or add additional context in comments.

1 Comment

To wit, the method the asker was looking for is isSubclassOfClass: rather than isKindOfClass:.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.