Objective c isKindOfClass missunderstanding?

前端 未结 3 1066
野趣味
野趣味 2020-12-05 18:56

I have following structure of objects:

Animal, Dog and Cat. As You expect Dog and Cat are inherited from Animal.

And I\'ve a farm class:

 @im         


        
3条回答
  •  感动是毒
    2020-12-05 19:34

    Your problem lies elsewhere.

    I created your Animal, Dog, and Cat classes, and the four cases you have above passed. For reference, here is my code: http://pastie.org/774468

    It outputs:

    2010-01-11 19:45:10.259 EmptyFoundation[83698:a0f] [cat isMemberOfClass:[Cat class]] PASSED
    2010-01-11 19:45:10.265 EmptyFoundation[83698:a0f] [dog isMemberOfClass:[Dog class]] PASSED
    2010-01-11 19:45:10.265 EmptyFoundation[83698:a0f] [cat isKindOfClass:[Animal class]] PASSED
    2010-01-11 19:45:10.273 EmptyFoundation[83698:a0f] [dog isKindOfClass:[Animal class]] PASSED
    

    EDIT:

    I suppose there was a slight possibility that your AnimalFarm object was the source of the error, but I just tried creating the animal objects that way and got the same results (code: http://pastie.org/774480):

    2010-01-11 19:51:35.144 EmptyFoundation[83741:a0f] [cat isMemberOfClass:[Cat class]] PASSED
    2010-01-11 19:51:35.156 EmptyFoundation[83741:a0f] [dog isMemberOfClass:[Dog class]] PASSED
    2010-01-11 19:51:35.157 EmptyFoundation[83741:a0f] ![ant isMemberOfClass:[Cat class]] PASSED
    2010-01-11 19:51:35.157 EmptyFoundation[83741:a0f] [cat isKindOfClass:[Animal class]] PASSED
    2010-01-11 19:51:35.157 EmptyFoundation[83741:a0f] [dog isKindOfClass:[Animal class]] PASSED
    2010-01-11 19:51:35.158 EmptyFoundation[83741:a0f] [ant isKindOfClass:[Animal class]] PASSED
    

    EDIT #2:

    Based on your observation that an if...else if statement works but the switch statement does not, I modified the code I posted above to use a switch statement.... and it worked just fine. So my comment/question stands: In your if/switch statements, you're using these constants DOG and CAT. Where are those defined?

提交回复
热议问题