1.id 的c代码,oc的class都是struct,包括block最终也是struct. id 可以表示任何对象,因为对象类型只有运行期才能确认。
struct objc_object {
Class isa;
} *id;
2. Class
typedef struct objc_class *Class;
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
}
3.SEL 是选择子的类型,选择子指的就是方法的名字。
typedef struct objc_selector *SEL;
4.typedef struct objc_method *Method; 代表类中的某个方法的类型
struct objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
}
5. Ivar代表类中实例变量的类型
typedef struct objc_ivar *Ivar;
struct objc_ivar {
char *ivar_name OBJC2_UNAVAILABLE;
char *ivar_type OBJC2_UNAVAILABLE;
int ivar_offset OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
}
6. objc_property_t是属性
typedef struct objc_property *objc_property_t;
class_copyPropertyList(Class cls, unsigned int *outCount) 获取某个类的属性列表。
id objc_msgSend (id self, SEL _cmd, ...); index=0,1,2...(接收者,选择子,参数)
对象在收到无法解读的消息后->resolveInstanceMethod->forwardingTargetForSelector->methodSignatureForSelector->forwardInvocation->doesNotRecognizeSelector
+ (BOOL)resolveInstanceMethod:(SEL)sel
{
return NO;
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return nil;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
if ([NSStringFromSelector(aSelector) isEqualToString:@"study"])
{
return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}
return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
[anInvocation setSelector:@selector(play)];
[anInvocation invokeWithTarget:self];
}
- (void)doesNotRecognizeSelector:(SEL)aSelector
{
NSLog(@"无法处理消息:%@", NSStringFromSelector(aSelector));
}
objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
id objc_getAssociatedObject(id object, void *key)
void objc_removeAssociatedObjects(id object)
class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
method_exchangeImplementations(Method m1, Method m2)
method_setImplementation(Method m, IMP imp)
参考文章:www.jianshu.com/p/3e050ec3b…