美文网首页
[iOS]消息转发-基元修改

[iOS]消息转发-基元修改

作者: 汴城码农 | 来源:发表于2020-07-03 14:39 被阅读0次

    工作笔记

    描述:我想要做的是将调用转发给一个或另一个选择器,具体取决于它是对象还是基本类型.最终目标是我想“封装”基元,以便将它们添加到数组/字典中
    /**
     * 类型对照表
     * https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
     */
    -(void)forwardInvocation:(NSInvocation *)anInvocation{
        if (anInvocation.methodSignature.numberOfArguments>2) {
            for (int i= 2; i<anInvocation.methodSignature.numberOfArguments; i++) {
                
    //            void * arg;
    //            [anInvocation getArgument:&arg atIndex:i];
    //            //id args = (__bridge id) arg;
    //            id args = (__bridge_transfer id)arg;
                
                __unsafe_unretained id argument;
                [anInvocation getArgument:&argument atIndex:2];
                            
                NSMethodSignature *signature = [anInvocation methodSignature];
                const char* argType = [signature getArgumentTypeAtIndex:i];
                const NSString *typeStr = [NSString stringWithFormat:@"%s",argType];
    
                if ([typeStr isEqualToString:@"q"]) { //目前所有参数都是NSString类型,如果weex传的参数类型不对,容错处理转为NSString
                    NSString * argumentStr= [NSString stringWithFormat:@"%qu",argument];
                    const char *cString = [argumentStr cStringUsingEncoding:NSUTF8StringEncoding];
                    [anInvocation setArgument:cString atIndex:i];
                }
            }
        }
    }
    

    参考

    相关文章

      网友评论

          本文标题:[iOS]消息转发-基元修改

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