-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
huangyibiao
authored and
huangyibiao
committed
Dec 31, 2015
1 parent
393a9d2
commit 83c78d8
Showing
10 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+2.44 KB
(110%)
RuntimeDemo.xcworkspace/xcuserdata/huangyibiao.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// HYBCat.h | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 15/12/31. | ||
// Copyright © 2015年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HYBCat : NSObject | ||
|
||
|
||
|
||
+ (void)test; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// HYBCat.m | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 15/12/31. | ||
// Copyright © 2015年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import "HYBCat.h" | ||
#import "HYBDog.h" | ||
#import "HYBPig.h" | ||
#import <objc/message.h> | ||
|
||
@implementation HYBCat | ||
|
||
// 第一步:在没有找到方法时,会先调用此方法,可用于动态添加方法 | ||
// 我们不动态添加 | ||
+ (BOOL)resolveInstanceMethod:(SEL)sel { | ||
return NO; | ||
} | ||
|
||
// 第二步:上一步返回NO,就会进入这一步,用于指定备选响应此SEL的对象 | ||
// 千万不能返回self,否则就会死循环 | ||
// 自己没有实现这个方法才会进入这一流程,因此成为死循环 | ||
- (id)forwardingTargetForSelector:(SEL)aSelector { | ||
return nil; | ||
} | ||
|
||
// 第三步:指定方法签名,若返回nil,则不会进入下一步,而是无法处理消息 | ||
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { | ||
if ([NSStringFromSelector(aSelector) isEqualToString:@"eat"]) { | ||
return [NSMethodSignature signatureWithObjCTypes:"v@:"]; | ||
} | ||
|
||
return [super methodSignatureForSelector:aSelector]; | ||
} | ||
|
||
// 当我们实现了此方法后,-doesNotRecognizeSelector:不会再被调用 | ||
// 如果要测试找不到方法,可以注释掉这一个方法 | ||
- (void)forwardInvocation:(NSInvocation *)anInvocation { | ||
|
||
// 我们还可以改变方法选择器 | ||
[anInvocation setSelector:@selector(jump)]; | ||
// 改变方法选择器后,还需要指定是哪个对象的方法 | ||
[anInvocation invokeWithTarget:self]; | ||
} | ||
|
||
- (void)doesNotRecognizeSelector:(SEL)aSelector { | ||
NSLog(@"无法处理消息:%@", NSStringFromSelector(aSelector)); | ||
} | ||
|
||
- (void)jump { | ||
NSLog(@"由eat方法改成jump方法"); | ||
} | ||
|
||
+ (void)test { | ||
HYBDog *dog = [[HYBDog alloc] init]; | ||
[dog eat]; | ||
|
||
HYBPig *pig = [[HYBPig alloc] init]; | ||
// [pig performSelector:@selector(eat) withObject:nil afterDelay:0]; | ||
((void (*)(id, SEL))objc_msgSend)((id)pig, @selector(eat)); | ||
|
||
HYBCat *cat = [[HYBCat alloc] init]; | ||
[cat performSelector:@selector(eat) withObject:nil afterDelay:0]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// HYBDog.h | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 15/12/31. | ||
// Copyright © 2015年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HYBDog : NSObject | ||
|
||
// 我们只声明,而不实现 | ||
- (void)eat; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// HYBDog.m | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 15/12/31. | ||
// Copyright © 2015年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import "HYBDog.h" | ||
#import <objc/runtime.h> | ||
|
||
|
||
@implementation HYBDog | ||
|
||
// 第一步:实现此方法,在调用对象的某方法找不到时,会先调用此方法,允许 | ||
// 我们动态添加方法实现 | ||
+ (BOOL)resolveInstanceMethod:(SEL)sel { | ||
// 我们这里没有给dog声明有eat方法,因此,我们可以动态添加eat方法 | ||
if ([NSStringFromSelector(sel) isEqualToString:@"eat"]) { | ||
class_addMethod(self, sel, (IMP)eat, "v@:"); | ||
return YES; | ||
} | ||
|
||
return [super resolveInstanceMethod:sel]; | ||
} | ||
|
||
// 这个方法是我们动态添加的哦 | ||
// | ||
void eat(id self, SEL cmd) { | ||
NSLog(@"%@ is eating", self); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// HYBPig.h | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 15/12/31. | ||
// Copyright © 2015年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HYBPig : NSObject | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// HYBPig.m | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 15/12/31. | ||
// Copyright © 2015年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import "HYBPig.h" | ||
#import "HYBDog.h" | ||
|
||
@implementation HYBPig | ||
|
||
// 第一步,我们不动态添加方法,返回NO | ||
+ (BOOL)resolveInstanceMethod:(SEL)sel { | ||
return NO; | ||
} | ||
|
||
// 第二步,备选提供响应aSelector的对象,我们不备选,因此设置为nil,就会进入第三步 | ||
- (id)forwardingTargetForSelector:(SEL)aSelector { | ||
return nil; | ||
} | ||
|
||
// 第三步,先返回方法选择器。如果返回nil,则表示无法处理消息 | ||
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { | ||
if ([NSStringFromSelector(aSelector) isEqualToString:@"eat"]) { | ||
return [NSMethodSignature signatureWithObjCTypes:"v@:"]; | ||
} | ||
|
||
return [super methodSignatureForSelector:aSelector]; | ||
} | ||
|
||
// 第三步,只有返回了方法签名,都会进入这一步,这一步用户调用方法 | ||
// 改变调用对象等 | ||
- (void)forwardInvocation:(NSInvocation *)anInvocation { | ||
// 我们改变调用对象为dog | ||
[anInvocation invokeWithTarget:[[HYBDog alloc] init]]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters