iPhone iOS how to compare Class object to another class object?

11,045

Solution 1

There are two methods you're interested in:

isKindOfClass: asks the receiver if it is a class or a subclass, where as isMemberOfClass: asks the receiver if it is the class, but not a subclass. For instance, let's say you have your NSManagedObject subclass called objectClass.

 if([self.objectClass isKindOfClass:[NSManagedObject class]]) {
     // This will return YES
 }
 else if ([self.objectClass isMemberOfClass:[NSManagedObject class]]) {
     // This will return NO
 }

The first statement returns YES (or true, or 1) because objectClass is a subclass of NSManagedObject. The second statement returns NO (or false, or 0) because while it is a subclass, it is not the class itself.

UPDATE: I'd like to update this answer to bring light to a comment below, which states that this explanation is wrong because the following line of code:

if ([self.class isKindOfClass:self.class])

would return false. This is correct, it would return false. But this example is wrong. Every class that inherits from NSObject also conforms to the NSObject protocol. Within this protocol is a method called class which "returns the class object for the receiver's class". In this case, self.class returns whatever class object self is. However, from the documentation on isKindOfClass: -

Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

thus, sending this message to self.class (which is a class) returns false because it is meant to be sent to an instance of a class, not to a class itself.

If you change the example to

if([self isKindOfClass:self.class])

You will get YES (or true, or 1).

My answer here presumes that self.objectClass is an accessor to an instance named objectClass. Sure, it's a terrible name for an instance of a class, but the question was not "how do I name instances of classes".

Solution 2

Yes, [self.objectClass isSubclassOfClass:[NSManagedObject class]] is correct. If it is false, then that means the class represented by self.objectClass is not a subclass of NSManagedObject. I don't understand what your problem is, or what you are expecting.

Share:
11,045
Alex Stone
Author by

Alex Stone

When people asked me what I wanted to do for work 10 years ago, I did not know too well, so I just said "Something to do with computers". As I look at the last 10 years, I see that I did quite a lot of work all kinds of computers. From fiddling with microcontrollers and assembler code to writing data processing scripts to physically assembling computer consoles. The big step forward came when I learned to think about software in a structured, object-oriented way, as this allowed me to make software do things that I want it to do. Right now I'm proficient in two high level programming languages - Objective-C and Java and have touched just about every framework available for iOS. I've also became a hacker/maker and have completed a number of projects involving software and hardware. Right now I'm in my early 30s and when I ask myself "What would I like to do in the next 10 years?", my answer is "something with the human brain". The seeds are already there - I've picked up an interest in biology, cognitive science and neuroscience, enough to converse with real people. I've done first-hand research into sleep and made discoveries. I've taken classes in synthetic biology, performing manipulations on the bacteria genome. I've learned a lot about the neurotransmitter systems of the human brain, as well as how a biological organism develops. It seems like there are a lot of similarities between the object-oriented concepts I use in the daily programming tasks and how biological organisms operate. This makes me hopeful that by the time I'm in my late 30s, I would be able to work and program some form of biological computer or just plain hack the human brain.

Updated on June 14, 2022

Comments

  • Alex Stone
    Alex Stone almost 2 years

    I have a Class reference defined in one of classes working with:

    Class _objectClass;
    
         if([self.objectClass isSubclassOfClass:[NSManagedObject class]])
            {
               //does not get called
            }
    

    How can I check what kind of Class I'm dealing with?

    UPDATE: sorry autocomplete did not show me that isKindOfClass: was available. I'm testing that now