iOS ,呼叫捕获抛出勉未知方法的障碍

iOS 捕获未知方法的调用,避勉抛出异常

太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的漂亮人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则。出自本博客的文章拒绝转载或再转载。谢谢合作。


NSObject 对象是 Objecitve-C 中的根类。其有下面两个方法,在调用 NSObject 及其子类的方法不存在时,会将这个调用封装成 NSInvocation * 类型,试图传递给 forwardInvocation: 方法,假设原方法调用的对象重载了forwardInvocation: 方法,forwardInvocation: 方法就会被调用。

forwardingTargetForSelector: 的真正用途。从官网的描写叙述中,还是未全然体会其可用的场景,仅仅是后一方法在做反射处理时到时用到过,參见 “iOS 实现的 json 数据源的 O-R Mapping”。


forwardingTargetForSelector:

返回未知消息首先应该转向的对象。


Returns the object to which unrecognized messages should first be directed.

- (id)forwardingTargetForSelector:(SEL)aSelector

forwardInvocation:

由子类重载。用于前转消息到其他对象。
Overridden by subclasses to forward messages to other objects.

- (void)forwardInvocation:(NSInvocation *)anInvocation
- (void)forwardInvocation:(NSInvocation *)invocation {
    
    SEL orignalSelector = [invocation selector];
    
    if ([friend respondsToSelector:orignalSelector]) {
        
        [invocation invokeWithTarget:friend];
    }
    
    else {
        
        [super forwardInvocation:invocation];
    }
}









版权声明:本文博主原创文章。博客,未经同意不得转载。转载联系 QQ 30952589,请具体说明他想加好友。

posted @ 2015-11-05 11:00  blfshiye  阅读(186)  评论(0编辑  收藏  举报