• iOS 利用runtime调用方法


    利用runtime调用方法,可实现不做import,直接调用

    // Build Setting--> Apple LLVM 6.0 - Preprocessing--> Enable Strict Checking of objc_msgSend Calls  改为 NO

    - (void)execFunction2

    {

        NSString *functionName = @"runWithFriend:";

        NSString *className = @"People";

        NSString *friend = @"妹子";

        

        SEL runAction = NSSelectorFromString(functionName);

        Class peopleClass = NSClassFromString(className);

        

        objc_msgSend(peopleClass, runAction, friend);

    }

     

    static NSMutableDictionary *cache;

    - (void)execFunction

    {

        if (!cache)

            cache = [NSMutableDictionary dictionary];

        

        NSString *functionName = @"runWithFriend:";

        NSString *className = @"People";

        NSString *friend = @"妹子";

        

        SEL runAction = NSSelectorFromString(functionName);

        Class peopleClass = NSClassFromString(className);

        

        NSMethodSignature *runSig;

        if ([cache objectForKey:functionName]) {

            runSig = [cache objectForKey:functionName];

        } else {

        // methodSignatureForSelector:比较耗费性能, 所以最好把签名缓存起来

            runSig = [peopleClass methodSignatureForSelector:runAction];

            [cache setObject:runSig forKey:functionName];

        }

     

        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:runSig];

        

        [inv setSelector:runAction];

        [inv setArgument:&friend atIndex:2];

        [inv invokeWithTarget:peopleClass];

    }

  • 相关阅读:
    洛谷 P1019单词接龙
    洛谷 P1091合唱队列
    洛谷 P1141 01迷宫
    洛谷 P1101单词方阵
    NOIP要炸?
    洛谷 P1219八皇后
    洛谷 P1181数列分段Section I
    刷普及-刷爆了。。。。。。
    洛谷 P3952时间复杂度 (本地AC测评RE的伪题解)
    动态数码管
  • 原文地址:https://www.cnblogs.com/oumygade/p/4641231.html
Copyright © 2020-2023  润新知