当前位置 博文首页 > xixi:NSSelectorFromString 的使用,根据字符串来调用相应名字

    xixi:NSSelectorFromString 的使用,根据字符串来调用相应名字

    作者:[db:作者] 时间:2021-08-07 12:54

    看代码

    [self doAction:@"testA" andArgObject:nil];
    [self doAction:@"testB:" andArgObject:@"xixi"];
    
    
    -(void) doAction:(NSString *)methodsName andArgObject:(id)objectArg
    {
        SEL selector = NSSelectorFromString(methodsName);
        if (self)
        {
            if ([self respondsToSelector:selector])
            {
                [self performSelectorOnMainThread:selector withObject:objectArg waitUntilDone:NO];
            }
        }
    }
    
    -(void) testA
    {
        NSLog(@"do testA");
    }
    
    -(void) testB:(NSString *) str
    {
        NSLog(@"do testB :%@",str);
    }
    



    结果 log

    2016-08-10 12:18:04.823 [94548:2783958] do testA
    2016-08-10 12:18:05.602 [94548:2783958] do testB :xixi


    cs
    下一篇:没有了