Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:11:17+00:00 2026-06-05T14:11:17+00:00

I am having problem understanding this part of Testing Class Equality which is defined

  • 0

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]) { //...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:11:18+00:00Added an answer on June 5, 2026 at 2:11 pm

    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]].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am studying apple obj-c guide and i am having problem understanding class types,
I'm having a problem understanding Java generics and I've simplified to this example class
i am having problem in understanding the behaviour of this programme below is simple
I'm new to JavaScript, and am having a problem understanding this code: function addProperty(o)
I'm having a little problem understanding variance of methods when overloading. While this perfectly
I have the following problem, and am having trouble understanding part of the equation:
I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence
I am having a problem understanding how polymorphism works when using generics. As an
i'm new to c++ and having a little problem understanding about c++'s casting. According
I'm quite new to OpenId and I'm having a bit of a problem understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.