ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

iOS performSelector多参数传递解决方案以及objc_msgSend的使用注意事项

2022-05-07 10:02:50  阅读:221  来源: 互联网

标签:performSelector nil iOS selector objc 参数 msgSend id


1.iOS performSelector多参数传递解决方案以及objc_msgSend的使用注意事项

 

https://blog.csdn.net/glt_code/article/details/77584683

 

iOS performSelector多参数传递解决方案


以及objc_msgSend的使用注意事项

 


iOS中使用performSelector:withObject:withObject:方法最多传递两个参数

[self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>]

 


解决方案:1. 使用NSInvocation进行消息转发从而实现对performSelector的多参数传递

2. 使用runtime中的objc_msgSend进行消息的发送

 

方案一:

以下是对NSObject类的扩展方法:

NS_REQUIRES_NIL_TERMINATION:是对多参数传递值得一个宏

va_list args:定义一个指向个数可变的参数列表指针;

va_start(args,object):object为第一个参数,也就是最右边的已知参数,这里就是获取第一个可选参数的地址.使参数列表指针指向函数参数列表中的第一个可选参数,函数参数列表中参数在内存中的顺序与函数声明时的顺序是一致的。

va_arg(args,id):返回参数列表中指针所指的参数,返回类型为id,并使参数指针指向参数列表中下一个参数。

va_end(args):清空参数列表,并置参数指针args无效。

其他说明见代码注释 Object分类

-(id)glt_performSelector:(SEL)selector withObject:(id)object,...NS_REQUIRES_NIL_TERMINATION;
{
    //根据类名以及SEL 获取方法签名的实例
    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
    if (signature == nil) {
        NSLog(@"--- 使用实例方法调用 为nil ---");
        signature = [self methodSignatureForSelector:selector];
        if (signature == nil) {
            NSLog(@"使用类方法调用 也为nil, 此时return");
            return nil;
        }
    }
    //NSInvocation是一个消息调用类,它包含了所有OC消息的成分:target、selector、参数以及返回值。
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.target = self;
    invocation.selector = selector;
    NSUInteger argCount = signature.numberOfArguments;
    // 参数必须从第2个索引开始,因为前两个已经被target和selector使用
    argCount = argCount > 2 ? argCount - 2 : 0;
    NSMutableArray *objs = [NSMutableArray arrayWithCapacity:0];
    if (object) {
        [objs addObject:object];
        va_list args;
        va_start(args, object);
        while ((object = va_arg(args, id))){
            [objs addObject:object];
        }
        va_end(args);
    }
    if (objs.count != argCount){
        NSLog(@"--- objs.count != argCount! please check it! ---");
        return nil;
    }
    //设置参数列表
    for (NSInteger i = 0; i < objs.count; i++) {
        id obj = objs[i];
        if ([obj isKindOfClass:[NSNull class]]) {
            continue;
        }
        [invocation setArgument:&obj atIndex:i+2];
    }
    [invocation invoke];
    //获取返回值
    id returnValue = nil;
    if (signature.methodReturnLength != 0 && signature.methodReturnLength) {
        [invocation getReturnValue:&signature];
    }
    return returnValue;
}
 
 

 


shareExtension调用

 UIResponder* responder = self;

    NSString *urlStr = [NSString stringWithFormat:@"scheme://host/path%@",url];

   while ((responder = [responder nextResponder]) != nil) {

       if ([responder respondsToSelector:@selector(openURL:)] == YES && [responder isKindOfClass:[UIApplication class]]) {

          [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:urlStr]];

      }else if([responder isKindOfClass:[UIApplication class]] && [responder respondsToSelector:@selector(openURL: options: completionHandler:)]) {

//           [responder performSelector:@selector(openURL: options: completionHandler:) withObject:[NSURL URLWithString:urlStr] withObject:@{}];

          void (^block)(void) = ^{

                  

              };

          [responder glt_performSelector:@selector(openURL: options: completionHandler:) withObject:[NSURL URLWithString:urlStr], @{},block,nil];

       }

    }

    [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];


注意:在Swift中没有NSMethodSignature和NSInvocation方法,暂时只能通过桥接解决,Swift中给出了这样一句:
NS_SWIFT_UNAVAILABLE("NSInvocation and related APIs not available")
如果在Swift中谁有好的方法可以扩展,请求大神告知!不胜感激!

方案二:

objc_msgSend :

objc_msgSend(<#id self#>, <#SEL op, ...#>)
概念:其实每个类中都有一张方法列表去存储这个类中有的方法,当发出objc_msgSend方法时候,就会顺着列表去找这个方法是否存在,如果不存在,则向该类的父类继续查找,直到找到位置。如果始终没有找到方法,那么就会进入到消息转发机制;objc_msgSend被分为2个过程:1)在cache中寻找SEL。2)在MethodTable寻找SEL。

具体使用介绍:

在一个列中有以下两个方法:

@implementation CustomClass

-(void)fun
{
NSLog(@"fun");
}

-(void)eat:(NSString *)food say:(NSString *)some
{
NSLog(@"%@ %@",food, some);
}

@end

当我们使用的时候,使用objc_msgSend进行调用,如下:这样便调用了类中的fun方法

TestClass *cls = [[TestClass alloc] init];

objc_msgSend(cls, @selector(fun)); //错误写法(arm64崩溃偶尔发生)

((void (*)(id, SEL))objc_msgSend)(cls, @selector(fun)); //正确写法

//具体原因见下面解释 objc_msgSend arm64 崩溃问题


使用注意1:使用objc_msgSend crash解决方案

 

 

使用注意2:objc_msgSend arm64 崩溃问题
之前一直用objc_msgSend,但是没注意apple的文档提示,所以突然objc_msgSend crash了。
按照文档 64-Bit Transition Guide for Cocoa Touch 给出了以下代码片段:

- (int) doSomething:(int) x { ... }
- (void) doSomethingElse {
int (*action)(id, SEL, int) = (int (*)(id, SEL, int)) objc_msgSend;
action(self, @selector(doSomething:), 0);
}
所以必须先定义原型才可以使用,这样才不会发生崩溃,调用的时候则如下:
void (*glt_msgsend)(id, SEL, NSString *, NSString *) = (void (*)(id, SEL, NSString *, NSString *))objc_msgSend;

glt_msgsend(cls, @selector(eat:say:), @"123", @"456");
————————————————
版权声明:本文为CSDN博主「高刘通」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/glt_code/article/details/77584683

 

标签:performSelector,nil,iOS,selector,objc,参数,msgSend,id
来源: https://www.cnblogs.com/sundaysgarden/p/16241244.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有