isa詳解
isa
isa位域
Union共用體實(shí)踐
- 通過(guò) & 和 | 運(yùn)算 簡(jiǎn)化傳值
typedef NS_OPTIONS(NSInteger,RereshTypeEnum) {
// 1 向左移動(dòng)的位數(shù)
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");
}
}
// 調(diào)用
Person * p = [[Person alloc] init];
[p setRefreshType:refreshOneType | refreshTwoType];
Class 方法緩存
image.png
objc_msgSend執(zhí)行流程
消息發(fā)送
消息發(fā)送
- 動(dòng)態(tài)方法解析
/**
實(shí)例方法的動(dòng)態(tài)解析
*/
- (void)testMethod{
NSLog(@"實(shí)例方法---動(dòng)態(tài)方法解析來(lái)到了");
}
+ (BOOL)resolveInstanceMethod:(SEL)sel{
if (sel == @selector(resolvenMethodTest)) {
//獲取到要?jiǎng)討B(tài)解析的調(diào)用方法
Method method = (Method)class_getInstanceMethod(self, @selector(testMethod));
//添加調(diào)用的方法锦庸,實(shí)現(xiàn)到解析的方法上去忿磅。
class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method));
//返回YES彼水,代表有動(dòng)態(tài)添加方法
return YES;
}
return [super resolveInstanceMethod:sel];
}
/**
類方法的動(dòng)態(tài)解析
*/
+ (void)classTestMethod{
NSLog(@"類方法---動(dòng)態(tài)方法解析來(lái)到了");
}
+(BOOL)resolveClassMethod:(SEL)sel{
if (sel == @selector(classResolvenMethodTest)) {
//獲取 動(dòng)態(tài)解析的類方法
Method method = (Method)class_getClassMethod(self, @selector(classTestMethod));
// object_getClass 獲取類對(duì)象
class_addMethod(object_getClass(self), sel, method_getImplementation(method), method_getTypeEncoding(method));
return YES;
}
return [super resolveClassMethod:sel];
}
動(dòng)態(tài)方法解析
- 消息轉(zhuǎn)發(fā)
/**
消息轉(zhuǎn)發(fā)
*/
- (id)forwardingTargetForSelector:(SEL)aSelector{
if (aSelector == @selector(forwardingMethodTest)) {
//返回可以進(jìn)行轉(zhuǎn)發(fā)的對(duì)象
Son * s= [[Son alloc] init];
return s;
}
return [super forwardingTargetForSelector:aSelector];
}
--------------------------------------------
- (id)forwardingTargetForSelector:(SEL)aSelector{
if (aSelector == @selector(forwardingMethodTest)) {
//如果返回nil,則執(zhí)行
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 封裝了方法的調(diào)用 調(diào)用者 方法名 參數(shù)
-(void)forwardInvocation:(NSInvocation *)anInvocation{
NSLog(@"%@",anInvocation);
NSLog(@"%@ %@",anInvocation.target,NSStringFromSelector(anInvocation.selector));
[anInvocation invokeWithTarget:[[Son alloc] init]];
}