美文网首页
Runtime之方法不存在处理

Runtime之方法不存在处理

作者: 飘摇的水草 | 来源:发表于2022-06-02 17:26 被阅读0次
找不到方法时处理

如下所示,当 person 对象找不到对应的 run 方法出现闪退时,可以使用 runtime 所学知识来处理

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    Person *person = [[Person alloc]init];
    [person run];
    
    return YES;
    
}

解决方案如下,在 person.m 中实现如下方法即可

@implementation Person

- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
    //本来能调用的方法
    if ([self respondsToSelector:selector])
    {
        return [super methodSignatureForSelector:selector];
    }
    //找不到方法
    return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    NSLog(@"找不到方法:%@",NSStringFromSelector(anInvocation.selector));
}

@end

如果要想所有的类找不到方法时均不出现闪退,则可以用 NSObject+分类 的方法实现上述方法来避免因找不到方法而出现闪退

相关文章

  • Runtime之方法不存在处理

    找不到方法时处理 如下所示,当 person 对象找不到对应的 run 方法出现闪退时,可以使用 runtime ...

  • 基础篇

    Runtime之必备C知识 Runtime之类的本质 Runtime之消息处理策略 Runtime之常用API 进...

  • Runtime

    # Runtime Runtime 不光能够进行方法交换,还能够在运行时处理 Objective-C 特性相关(比...

  • iOS Runtime之方法查找

    Runtime系列导读 iOS Runtime之方法查找[https://www.jianshu.com/p/f6...

  • iOS Runtime之方法替换

    Runtime系列导读 iOS Runtime之方法查找[https://www.jianshu.com/p/f6...

  • iOS Runtime之KVO

    Runtime系列导读 iOS Runtime之方法查找[https://www.jianshu.com/p/f6...

  • iOS Runtime之反射调用

    Runtime系列导读 iOS Runtime之方法查找[https://www.jianshu.com/p/f6...

  • iOS Runtime之KVC

    Runtime系列导读 iOS Runtime之方法查找[https://www.jianshu.com/p/f6...

  • Selector & SEL

    Objective-C中的Runtime 消息处理之performSelector IOS SEL (@selec...

  • iOS开发 Rumtime运行时之消息转发机制(三)

    转载自:IOS开发工程师--周玉的博客 iOS 开发 深入浅出Runtime运行时之官方翻译--动态方法处理 iO...

网友评论

      本文标题:Runtime之方法不存在处理

      本文链接:https://www.haomeiwen.com/subject/wybqmrtx.html