isa详解
Union共用体实践
- 通过 & 和 | 运算 简化传值
typedef NS_OPTIONS(NSInteger,RereshTypeEnum) {
// 1 向左移动的位数
refreshOneType = 1 << 0,//0b0001
refreshTwoType = 1 << 1,//0b0010
refreshThreeType = 1 << 2,//0b100
refreshFourType = 1 << 3,//0b1000
};
-(void)setRefreshType:(RereshTypeEnum)type{
if (type&refreshOneType) {
NSLog(@"包含one");
}
if (type&refreshTwoType) {
NSLog(@"包含two");
}
if (type&refreshThreeType) {
NSLog(@"包含three");
}
if (type&refreshFourType) {
NSLog(@"包含four");
}
}
// 调用
Person * p = [[Person alloc] init];
[p setRefreshType:refreshOneType | refreshTwoType];
Class 方法缓存
objc_msgSend执行流程
消息发送
- 动态方法解析
/**
实例方法的动态解析
*/
- (void)testMethod{
NSLog(@"实例方法---动态方法解析来到了");
}
+ (BOOL)resolveInstanceMethod:(SEL)sel{
if (sel == @selector(resolvenMethodTest)) {
//获取到要动态解析的调用方法
Method method = (Method)class_getInstanceMethod(self, @selector(testMethod));
//添加调用的方法,实现到解析的方法上去。
class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method));
//返回YES,代表有动态添加方法
return YES;
}
return [super resolveInstanceMethod:sel];
}
/**
类方法的动态解析
*/
+ (void)classTestMethod{
NSLog(@"类方法---动态方法解析来到了");
}
+(BOOL)resolveClassMethod:(SEL)sel{
if (sel == @selector(classResolvenMethodTest)) {
//获取 动态解析的类方法
Method method = (Method)class_getClassMethod(self, @selector(classTestMethod));
// object_getClass 获取类对象
class_addMethod(object_getClass(self), sel, method_getImplementation(method), method_getTypeEncoding(method));
return YES;
}
return [super resolveClassMethod:sel];
}
- 消息转发
/**
消息转发
*/
- (id)forwardingTargetForSelector:(SEL)aSelector{
if (aSelector == @selector(forwardingMethodTest)) {
//返回可以进行转发的对象
Son * s= [[Son alloc] init];
return s;
}
return [super forwardingTargetForSelector:aSelector];
}
--------------------------------------------
- (id)forwardingTargetForSelector:(SEL)aSelector{
if (aSelector == @selector(forwardingMethodTest)) {
//如果返回nil,则执行
return nil;
}
return [super forwardingTargetForSelector:aSelector];
}
- (id)forwardingTargetForSelector:(SEL)aSelector{
if (aSelector == @selector(forwardingMethodTest)) {
return nil;
}
return [super forwardingTargetForSelector:aSelector];
}
//NSMethodSignature 方法签名 包括方法的返回值 和
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
if (aSelector == @selector(forwardingMethodTest)) {
return [[[Son alloc] init] methodSignatureForSelector:aSelector];
}
return [super methodSignatureForSelector:aSelector];
}
// NSInvocation 封装了方法的调用 调用者 方法名 参数
-(void)forwardInvocation:(NSInvocation *)anInvocation{
NSLog(@"%@",anInvocation);
NSLog(@"%@ %@",anInvocation.target,NSStringFromSelector(anInvocation.selector));
[anInvocation invokeWithTarget:[[Son alloc] init]];
}