Message Forwarding
- (void)forwardInvocation:(NSInvocation *)anInvocation {
if ([someOtherObject respondsToSelector:
[anInvocation selector]])
// Advice here
[anInvocation invokeWithTarget:someOtherObject];
// Advice here
else
[super forwardInvocation:anInvocation];
}
See: Objective-C Runtime Programming Guide
Thursday, September 12,
Slide 24
Slide 24 text
AOP-in-Objective-C
https://github.com/moszi/AOP-in-
Objective-C
Thursday, September 12,
Slide 25
Slide 25 text
Method
Swizzling
Thursday, September 12,
Slide 26
Slide 26 text
Objective-C 第⼀一課
• ObjC 物件都是 C Structure
• ObjC method 都是 C function pointer
• 系統有⼀一個動態的索引表格,決定執⾏行
某個 selector 時,要對應到哪個 C
function
• Selector 就是索引表中的 key,型態為 C
字串
Thursday, September 12,
Slide 27
Slide 27 text
Objective-C 第⼆二課
• ⼀一個 Class 有哪些 method,都是在
runtime 的時候建⽴立的
• 所以可以在 runtime 新增新的 method,
例如使⽤用 category 語法
• 也可以直接使⽤用 runtime API 把已經存在
的 method 換掉
Thursday, September 12,
Slide 28
Slide 28 text
BOOL class_addMethod(Class cls, SEL name,
IMP imp, const char *types);
IMP method_setImplementation(Method method,
IMP imp);
Thursday, September 12,