isMemberOfClass
- The caller must be passed an object class instance does not return YES
- determines whether the caller of the incoming object instance, do not backwards, such as [s1 isMemberOfClass: p1], meaning that if p1 is an instance of an object s1
- over and over again to determine the parent class recursive search
Source:
+ (BOOL)isMemberOfClass:(Class)cls { return object_getClass((id)self) == cls; } - (BOOL)isMemberOfClass:(Class)cls { return [self class] == cls; }
There are two methods, a class method, an object method, the two differences:
- Examples of the method: the object is an instance of the object class acquisition, go determination
- Method Object: The objects are achieved metaclass object is determined again
Example code:
* = P1 XPerson [[XPerson the alloc] the init]; XStudent * s1 = [[XStudent the alloc] the init]; // to true (s1-class objects and [s1 class] determination, is the same as the affirmative) NSLog ( @ " s1 s1 is whether examples:% I " , [s1 isMemberOfClass: [s1 class ]]); // to true ([s1 class] and [XStudent class] equivalents, only one class has a class object, a metaclass object objects can have multiple instances) NSLog ( @ " if s1 is XStudent example:% I " , [s1 isMemberOfClass: [XStudent class ]]); // ! class object to false (= s1 of the class object p1) NSLog ( @ " S1 whether p1 example:% I " , [S1 isMemberOfClass: [p1 class ]]); // to false (supra) NSLog ( @ " if s1 is XPerson Example:% I " , [s1 isMemberOfClass: [XPerson class ]]); // to false (underlying acquisition is to compare the metaclass XStudent) (= XStudent XStudent metaclass object is a class object!) NSLog ( @ " whether XStudent is XStudent example:% I " , [XStudent isMemberOfClass: [XStudent class ]]); // to true (= XStudent objects XStudent metaclass metaclass objects) NSLog ( @ " XStudent whether the metaclass instance XStudent :% I " , [XStudent isMemberOfClass: object_getClass ([XStudent class ])]); // ! to false (= XPerson objects XStudent metaclass metaclass objects) NSLog ( @" XStudent whether XPerson metaclass instance:% I ",[XStudent isMemberOfClass:
isKindOfClass
- the caller is an instance of an object class passed, or the caller is an instance of an object class passed successor in the chain class, returns YES
- determines whether the caller is a subclass of the incoming object, not backwards
- no parent recursively to find the judge
Source:
+ (BOOL)isKindOfClass:(Class)cls { for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->super_class) { if(tcls == cls) return YES; } return NO; } -(BOOL)isKindOfClass:(Class)cls { for(Class tcls = [self class]; tcls; tcls = tcls->super_class) { if(tcls == cls) return YES; } return NO; }
Example code:
* = P1 XPerson [[XPerson the alloc] the init]; XStudent * s1 = [[XStudent the alloc] the init]; // to true (s1 with a class object, the parent class object, the base class objects ([NSObject class]) to and comparing the class object p1, [p1 class] s1 is the parent class object) NSLog ( @ " s1 whether p1 subclass:% I " , [s1 the isKindOfClass: [p1 class ]]); // to true (ibid. : [XPerson class] and [p1 class] are equivalent) NSLog ( @ " s1 whether XPerson subclass:% I " , [s1 the isKindOfClass: [XPerson class ]]); // to true ([the NSObject class] are s1 the base class objects) NSLog ( @ " if s1 is a subclass of NSObject:% I " , [s1 the isKindOfClass: [NSObject class ]]); //to false (, parent metaclass, a base class (the NSObject class) to compare the XPerson object metaclass XStudent a) NSLog ( @ " XStudent whether XPerson subclass:% I " , [XStudent The isKindOfClass: [XPerson class ]] ); // to true (metaclass class methods need to pass into the determination, which will be taken to compare XStudent metaclass) NSLog ( @ " XStudent whether XPerson membered subclass:% I " , [XStudent The isKindOfClass: object_getClass ([XPerson class ])]); // to true (uppermost class is primitive metaclass) NSLog ( @ " XStudent whether membered NSObject subclass:% I " , [XStudent the isKindOfClass: object_getClass ([NSObject class ])]) ;