NSObject
@protocol NSObject
//...
@property (readonly) Class superclass;
- (Class)class OBJC_SWIFT_UNAVAILABLE("use 'anObject.dynamicType' instead");
- (instancetype)self;
- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
//...
@end
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}
//...
+ (BOOL)isSubclassOfClass:(Class)aClass;
+ (Class)superclass;
+ (Class)class OBJC_SWIFT_UNAVAILABLE("use 'aClass.self' instead");
//...
@end
应用
- (void)use_nsobject
{
Class objectClsObj = [NSObject class];
Class animalClsObj = [FBAnimal class];
Class dogClsObj = [FBDog class];
NSLog(@"objectClsObj = %p, animalClsObj = %p, dogClsObj = %p", objectClsObj, animalClsObj, dogClsObj);
NSLog(@"NSObject superclass = %p, FBAnimal superclass = %p, FBDog superclass = %p", [NSObject superclass], [FBAnimal superclass], [FBDog superclass]);
NSLog(@"FBDog isSubclassOfClass FBDog = %d", [FBDog isSubclassOfClass:dogClsObj]);
NSLog(@"FBDog isSubclassOfClass FBAnimal = %d", [FBDog isSubclassOfClass:animalClsObj]);
NSLog(@"FBDog isSubclassOfClass NSObject = %d", [FBDog isSubclassOfClass:objectClsObj]);
FBAnimal *animal1 = [[FBAnimal alloc] init];
FBAnimal *animal2 = [[FBDog alloc] init];
FBDog* dog = [[FBDog alloc] init];
NSLog(@"animal1 object = %p, animal1 self = %p, animal1 class = %p", animal1, [animal1 self], [animal1 class]);
NSLog(@"animal2 object = %p, animal2 self = %p, animal2 class = %p", animal2, [animal2 self], [animal2 class]);
NSLog(@"dog object = %p, dog self = %p, dog class = %p", dog, [dog self], [dog class]);
NSLog(@"animal1 isKindOfClass FBAnimal = %d", [animal1 isKindOfClass: animalClsObj]);
NSLog(@"animal1 isKindOfClass FBDog = %d", [animal1 isKindOfClass: dogClsObj]);
NSLog(@"animal1 isMemberOfClass FBAnimal = %d", [animal1 isMemberOfClass: animalClsObj]);
NSLog(@"animal1 isMemberOfClass FBDog = %d", [animal1 isMemberOfClass: dogClsObj]);
NSLog(@"animal2 isKindOfClass FBAnimal = %d", [animal2 isKindOfClass: animalClsObj]);
NSLog(@"animal2 isKindOfClass FBDog = %d", [animal2 isKindOfClass: dogClsObj]);
NSLog(@"animal2 isMemberOfClass FBAnimal = %d", [animal2 isMemberOfClass: animalClsObj]);
NSLog(@"animal2 isMemberOfClass FBDog = %d", [animal2 isMemberOfClass: dogClsObj]);
NSLog(@"dog isKindOfClass FBAnimal = %d", [dog isKindOfClass: animalClsObj]);
NSLog(@"dog isKindOfClass FBDog = %d", [dog isKindOfClass: dogClsObj]);
NSLog(@"dog isMemberOfClass FBAnimal = %d", [dog isMemberOfClass: animalClsObj]);
NSLog(@"dog isMemberOfClass FBDog = %d", [dog isMemberOfClass: dogClsObj]);
}
output:
objectClsObj = 0x101083170, animalClsObj = 0x100822d70, dogClsObj = 0x100822dc0
NSObject superclass = 0x0, FBAnimal superclass = 0x101083170, FBDog superclass = 0x100822d70
FBDog isSubclassOfClass FBDog = 1
FBDog isSubclassOfClass FBAnimal = 1
FBDog isSubclassOfClass NSObject = 1
animal1 object = 0x7fc7e37d4af0, animal1 self = 0x7fc7e37d4af0, animal1 class = 0x100822d70
animal2 object = 0x7fc7e37d68f0, animal2 self = 0x7fc7e37d68f0, animal2 class = 0x100822dc0
dog object = 0x7fc7e37d6730, dog self = 0x7fc7e37d6730, dog class = 0x100822dc0
animal1 isKindOfClass FBAnimal = 1
animal1 isKindOfClass FBDog = 0
animal1 isMemberOfClass FBAnimal = 1
animal1 isMemberOfClass FBDog = 0
animal2 isKindOfClass FBAnimal = 1
animal2 isKindOfClass FBDog = 1
animal2 isMemberOfClass FBAnimal = 0
animal2 isMemberOfClass FBDog = 1
dog isKindOfClass FBAnimal = 1
dog isKindOfClass FBDog = 1
dog isMemberOfClass FBAnimal = 0
dog isMemberOfClass FBDog = 1
总结:
- protocol中为instance method,是对instance object的Class isa操作
- interface中class method,是对class object操作
- isSubclassOfClass:本类和子类都属于subclass
- isKindOfClass:本类和子类都属于kind
- isMemberOFClass:本类属于Member,子类不属于member
NSObject封装
@protocol NSObject
//...
@property (readonly) Class superclass;
- (Class)class OBJC_SWIFT_UNAVAILABLE("use 'anObject.dynamicType' instead");
- (instancetype)self;
- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
//...
@end
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}
//...
+ (BOOL)isSubclassOfClass:(Class)aClass;
+ (Class)superclass;
+ (Class)class OBJC_SWIFT_UNAVAILABLE("use 'aClass.self' instead");
//...
@end
应用
- (void)use_nsobject
{
Class objectClsObj = [NSObject class];
Class animalClsObj = [FBAnimal class];
Class dogClsObj = [FBDog class];
NSLog(@"objectClsObj = %p, animalClsObj = %p, dogClsObj = %p", objectClsObj, animalClsObj, dogClsObj);
NSLog(@"NSObject superclass = %p, FBAnimal superclass = %p, FBDog superclass = %p", [NSObject superclass], [FBAnimal superclass], [FBDog superclass]);
NSLog(@"FBDog isSubclassOfClass FBDog = %d", [FBDog isSubclassOfClass:dogClsObj]);
NSLog(@"FBDog isSubclassOfClass FBAnimal = %d", [FBDog isSubclassOfClass:animalClsObj]);
NSLog(@"FBDog isSubclassOfClass NSObject = %d", [FBDog isSubclassOfClass:objectClsObj]);
FBAnimal *animal1 = [[FBAnimal alloc] init];
FBAnimal *animal2 = [[FBDog alloc] init];
FBDog* dog = [[FBDog alloc] init];
NSLog(@"animal1 object = %p, animal1 self = %p, animal1 class = %p", animal1, [animal1 self], [animal1 class]);
NSLog(@"animal2 object = %p, animal2 self = %p, animal2 class = %p", animal2, [animal2 self], [animal2 class]);
NSLog(@"dog object = %p, dog self = %p, dog class = %p", dog, [dog self], [dog class]);
NSLog(@"animal1 isKindOfClass FBAnimal = %d", [animal1 isKindOfClass: animalClsObj]);
NSLog(@"animal1 isKindOfClass FBDog = %d", [animal1 isKindOfClass: dogClsObj]);
NSLog(@"animal1 isMemberOfClass FBAnimal = %d", [animal1 isMemberOfClass: animalClsObj]);
NSLog(@"animal1 isMemberOfClass FBDog = %d", [animal1 isMemberOfClass: dogClsObj]);
NSLog(@"animal2 isKindOfClass FBAnimal = %d", [animal2 isKindOfClass: animalClsObj]);
NSLog(@"animal2 isKindOfClass FBDog = %d", [animal2 isKindOfClass: dogClsObj]);
NSLog(@"animal2 isMemberOfClass FBAnimal = %d", [animal2 isMemberOfClass: animalClsObj]);
NSLog(@"animal2 isMemberOfClass FBDog = %d", [animal2 isMemberOfClass: dogClsObj]);
NSLog(@"dog isKindOfClass FBAnimal = %d", [dog isKindOfClass: animalClsObj]);
NSLog(@"dog isKindOfClass FBDog = %d", [dog isKindOfClass: dogClsObj]);
NSLog(@"dog isMemberOfClass FBAnimal = %d", [dog isMemberOfClass: animalClsObj]);
NSLog(@"dog isMemberOfClass FBDog = %d", [dog isMemberOfClass: dogClsObj]);
}
output:
objectClsObj = 0x101083170, animalClsObj = 0x100822d70, dogClsObj = 0x100822dc0
NSObject superclass = 0x0, FBAnimal superclass = 0x101083170, FBDog superclass = 0x100822d70
FBDog isSubclassOfClass FBDog = 1
FBDog isSubclassOfClass FBAnimal = 1
FBDog isSubclassOfClass NSObject = 1
animal1 object = 0x7fc7e37d4af0, animal1 self = 0x7fc7e37d4af0, animal1 class = 0x100822d70
animal2 object = 0x7fc7e37d68f0, animal2 self = 0x7fc7e37d68f0, animal2 class = 0x100822dc0
dog object = 0x7fc7e37d6730, dog self = 0x7fc7e37d6730, dog class = 0x100822dc0
animal1 isKindOfClass FBAnimal = 1
animal1 isKindOfClass FBDog = 0
animal1 isMemberOfClass FBAnimal = 1
animal1 isMemberOfClass FBDog = 0
animal2 isKindOfClass FBAnimal = 1
animal2 isKindOfClass FBDog = 1
animal2 isMemberOfClass FBAnimal = 0
animal2 isMemberOfClass FBDog = 1
dog isKindOfClass FBAnimal = 1
dog isKindOfClass FBDog = 1
dog isMemberOfClass FBAnimal = 0
dog isMemberOfClass FBDog = 1
总结:
- protocol中为instance method,是对instance object的Class isa操作
- interface中class method,是对class object操作
- isSubclassOfClass:本类和子类都属于subclass
- isKindOfClass:本类和子类都属于kind
- isMemberOFClass:本类属于Member,子类不属于member
#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void method_invoke(void /* id receiver, Method m, ... */ )
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT void method_invoke_stret(void /* id receiver, Method m, ... */ )
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0)
OBJC_ARM64_UNAVAILABLE;
#else
OBJC_EXPORT id method_invoke(id receiver, Method m, ...)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT void method_invoke_stret(id receiver, Method m, ...)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0)
OBJC_ARM64_UNAVAILABLE;
#endif
#if !OBJC_OLD_DISPATCH_PROTOTYPES
OBJC_EXPORT void method_invoke(void /* id receiver, Method m, ... */ )
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT void method_invoke_stret(void /* id receiver, Method m, ... */ )
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0)
OBJC_ARM64_UNAVAILABLE;
#else
OBJC_EXPORT id method_invoke(id receiver, Method m, ...)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT void method_invoke_stret(id receiver, Method m, ...)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0)
OBJC_ARM64_UNAVAILABLE;
#endif
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp,
const char *types)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);