zoukankan      html  css  js  c++  java
  • 第16月第8天 NSInvocation存储 函数指针 va_arg lldb

    1.NSInvocation存储

    -(void)setInvok:(id)target sel:(SEL)sel key:(id)key
    {
        if(!target) return;
        NSMethodSignature *sig=[target methodSignatureForSelector:sel];
        NSInvocation *invo=[NSInvocation invocationWithMethodSignature:sig];
        [invo setTarget:target];
        [invo setSelector:sel];
        
        NSDictionary *dict = [self currentStyleDict];
        [dict setValue:invo forKey:key];//以key的形式保存invo
    }
    
    -(NSInvocation*)selectInvok:(NSString *)key style:(PALiveStyle)style
    {
        NSDictionary *dict = [self getStyleDict:style];
        return [dict valueForKey:key];
    }

    http://blog.sina.com.cn/s/blog_4beb28f30100qask.html

    2.函数指针

    //常规写法
    switch ( oper ){
        case ADD:
            result = add( oper1, oper2 );
            break;
        case SUB:
            result = sub( oper1, oper2 );
            break;
        case MUL:
            result = mul( oper1, oper2 );
            break;
        case DIV:
            result = div( oper1, oper2 );
            break;
        ...
    }
    //使用转移表之后的写法
    double add( double, double );
    double sub( double, double );
    double mul( double, double );
    double div( double, double );
    ...
    double (*oper_func[])( double, double ) = {
        add, sub, mul, div, ...
    };
    //下面的语句替换前面整条switch语句
    result = oper_func[ oper ]( op1, op2 );

    https://segmentfault.com/a/1190000000578532

    3.runtime

    http://justsee.iteye.com/blog/2163777

     4.调用是arg后记得传nil

    - (bool)startPlayingStream:(NSString *)streamID inView:(UIView*)view
    {
    //    NSInvocation *invo=[[MessageInvok shareInvok] selectInvok:@"start_playing_stream" style:PALiveStyleNormal];
    //    if(invo)
    //    {
    ////        [data retain];
    //        [invo setArgument:&streamID atIndex:2];  //设置参数
    //        [invo setArgument:&view atIndex:3];  //设置参数
    //        [invo invoke];   //调用
    //    }
    //    [self invokeMethod:@"start_playing_stream" style:PALiveStyleNormal,streamID,view];
        [self invokeMethod:@"start_playing_stream" style:PALiveStyleNormal arg:streamID,view, nil];
    
        return true;
    }
    
    - (bool)stopPlayingStream:(NSString *)streamID
    {
        
        return true;
    }
    
    -(NSInvocation *)invokeMethod:(NSString *)method style:(PALiveStyle)style arg:(id)arg, ...
    {
        NSMutableArray *arrayList = [NSMutableArray array];
        
        id eachItem;
        va_list argumentList;
        if (arg)
        {
            [arrayList addObject: arg];
            va_start(argumentList, arg);
            while((eachItem = va_arg(argumentList, id)))
            {
                [arrayList addObject: eachItem];
            }
            va_end(argumentList);
        }
        
        NSInvocation *invo=[[MessageInvok shareInvok] selectInvok:@"start_playing_stream" style:style];
        if(invo)
        {
            for (NSUInteger i =0; i<arrayList.count; i++) {
                NSInteger idx = i+2;
                id data = arrayList[i];
                [invo setArgument:&data atIndex:idx];
            }
            
            [invo invoke];   //调用
        }
        
        return invo;
    }

     5.lldb e

    e aView.backgroundColor = [UIColor greenColor];

     

    http://yulingtianxia.com/blog/2015/11/13/Summary-of-the-first-month-in-the-internship-of-Tencent/

    https://github.com/opensource-apple/objc4/blob/master/runtime/Messengers.subproj/objc-msg-x86_64.s

  • 相关阅读:
    MacOS Catalina 10.15安装教程,启动U盘制作及安装方法
    苹果macOS Catalina 10.15 正式版推送了,要不要升级,需要注意什么?
    Sublime Text Mac如何进行按键绑定?
    Mac软件应用程序打开出现意外退出、崩溃解决办法
    使用VScode Mac版编译配置C/C++程序完整图文教程
    pycharm pro 2019 mac重构技巧?
    一体化数据库管理Navicat Premium for Mac的命令提示代码
    jsonp理解
    linux系统命令大全
    java学习笔记之分层增删改查
  • 原文地址:https://www.cnblogs.com/javastart/p/8242638.html
Copyright © 2011-2022 走看看