29

I am having problem understanding this part of "Testing Class Equality" which is defined in apple guide.

In a dynamically-created subclass, the class method is typically overridden such that the subclass masquerades as the class it replaces. When testing for class equality, you should therefore compare the values returned by the class method rather than those returned by lower-level functions. Put in terms of API, the following inequalities pertain for dynamic subclasses:

[object class] != object_getClass(object) != *((Class*)object)

You should therefore test two classes for equality as follows:

if ([objectA class] == [objectB class]) { //...
5
  • 6
    or isMemberOfClass - stackoverflow.com/questions/383978/…
    – 9dan
    Jun 8, 2012 at 7:13
  • 1
    What's your goal here? Are you testing the class of one object, or comparing the classes of two objects, or...? Certainly *((Class *)object) won't equal the result of either of the procedures for getting the class object -- you can't just cast an instance to a class (also, the Class type is already a pointer).
    – jscs
    Jun 8, 2012 at 7:14
  • sorry i forgot to mention its from apple guide, and i am not getting this whole testing class equality thing... this whole para is from apple guide...
    – S.J
    Jun 8, 2012 at 7:18
  • 1
    @JoshCaswell nope, that works. We have typedef struct { Class isa } *id; so *((Class *)object) perfectly returns the isa (class) of the object.
    – user529758
    Jun 8, 2012 at 7:24
  • 1
    @S.J: I think you answered your own question. How people know advanced stuff is by doing it all day, every day for years and years. I suppose a thirst to improve on one's own methods and to be able to continually accomplish more with less effort doesn't hurt either. May 3, 2013 at 0:28

3 Answers 3

43

There are situations in which people add new classes at runtime. One example is Key Value Observing: when you observe an object the Foundation framework creates a new subclass of the observed object's class. This dynamic class behaves in the same way as its superclass, but adds KVO notifications to all of its mutator methods.

The passage you quoted says that the Objective-C runtime can tell this new class apart from the original class. However, because it's just an implementation detail of the way KVO is built, you shouldn't know or care about it. The developers therefore overrode the -class method of their new class, to pretend that objects were still members of the original class.

If you want to check whether two objects are of the same class, you must therefore compare the results of their -class methods (which take tricks like KVO into account), instead of using runtime functions.

Here's an example:

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSObject *observer = [NSObject new];
        NSObject *model = [NSObject new];

        [model addObserver: observer forKeyPath: @"count" options: 0 context: NULL];

        //using -class methods:
        NSLog(@"model is a %@, observer is a %@", [model class], [observer class]);

        //casting to Class:
        NSLog(@"model is a %@, observer is a %@", *(Class*)model, *(Class*)observer);

        //using the runtime:
        NSLog(@"model is a %@, observer is a %@", object_getClass(model), object_getClass(observer));

        [model removeObserver: observer forKeyPath: @"count" context: NULL];
        [model release];
        [observer release];
    }
    return 0;
}

You see that all I'm doing is creating two objects, telling one of them to observe the other, then finding out what their classes are. Here are the results:

2012-06-08 08:37:26.904 Untitled 2[896:707] model is a NSObject, observer is a NSObject

2012-06-08 08:37:26.907 Untitled 2[896:707] model is a NSKVONotifying_NSObject, observer is a NSObject

2012-06-08 08:37:26.907 Untitled 2[896:707] model is a NSKVONotifying_NSObject, observer is a NSObject

So as the documentation suggests, it's only the first case (where we compare -class) that does anything the application code could reasonably expect. The other two ways of finding out the class - asking the runtime, and casting the object pointer to a Class * - both give away implementation details about how KVO has changed the class from underneath us, and mean that the class comparison now won't show that the classes are equal.

Because other answers and comments are referring to -isMemberOfClass: and -isKindOfClass:, I'll cover those points too:

  • -isKindOfClass: is not a test for class equality. [object isKindOfClass: aClass] is true if object is an instance of aClass or any of its subclasses. Because the passage you've quoted is about class equality, -isKindOfClass: is not relevant here. That said, it's most often the test that you want to be doing in application code. It's more common to care about the answer to "can I use this object as a Foo?" than "is this object an instance exactly of Foo?".

  • -isMemberOfClass: is a test for class equality: [object isMemberOfClass: aClass] is only true if object is an instance of aClass. This test is done using the result of the -class method, which means that in this example model will test positive for [model isMemberOfClass: [NSObject class]].

2
  • Is *(Class*)model preferred over *(model->isa)? Jun 9, 2012 at 4:38
  • 1
    Neither is preferred, as both rely on breaking encapsulation in a way that -class does not.
    – user23743
    Jun 12, 2012 at 20:25
3

It should be:

if([objctA isKindOfClass [MyClass class]])
1
  • 7
    That's a different test. isKindOfClass: doesn't test for class equality, it tests that the tested object is a member of the specified class or any subclass.
    – user23743
    Jun 8, 2012 at 7:30
1

Maybe can you use the 'NSStringFromClass' method and compare the obtained strings thanks to 'isEqualToString' ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.