• iOS runtime


    —参考文章—

    • 暂时没有

    一、如何在Xcode中使用runtime

    Xcode默认是不建议开发者使用runtime的,所以在Xcode直接使用runtime的语法是会报错误的。
    如果要在Xcode中使用runtime的语法,是需要配置一下才可以使用,配置方法如下图:

    • 首先在1的位置搜索Enable strict
    • 默认是选中Yes的,然后只要选中No即可,然后在项目中使用runtime语法就不会报错误了

    配置runtime使用开关

    二、几个常用的语法

    • 获取当前对象的所有方法
    /* 获取对象的所有方法 */
    -(NSArray *)getAllMethods
    {
        NSMutableArray *tempMuArr = [[NSMutableArray alloc] init];
        unsigned int methCount = 0;
        Method *meths = class_copyMethodList([self class], &methCount);
        
        for(int i = 0; i < methCount; i++) {
            
            Method meth = meths[i];
            
            SEL sel = method_getName(meth);
            
            const char *name = sel_getName(sel);
            
            NSLog(@"%s", name);
            [tempMuArr addObject:[NSString stringWithFormat:@"%s", name]];
        }
        
        free(meths);
        
        return [tempMuArr copy];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 获取当前对象的所有属性
    /* 获取对象的所有属性 */
    - (NSArray *)getAllProperties
    {
        u_int count;
        
        objc_property_t *properties  = class_copyPropertyList([self class], &count);
        
        NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
        
        for (int i = 0; i < count ; i++)
        {
            const char* propertyName =property_getName(properties[i]);
            [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
        }
        
        free(properties);
        
        return propertiesArray;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 调用objc_msgSend方法
    //调用对象方法
    objc_msgSend(tempIamge, @selector(drawInRect:), CGRectMake(0, 0, 1242, 2208));
    
    
    //调用类方法
    //方式1
    UIImage *tempImage = ((UIImage *(*)(id, SEL, NSString *)) objc_msgSend)((id)[UIImage class], @selector(imageNamed:), @"test.jpg");
    //方式2
    UIImage *tempImage = ((UIImage *(*)(id, SEL, NSString *)) objc_msgSend)((id)objc_getClass("UIImage"), sel_registerName("imageNamed:"), @"test.jpg");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    无旋Treap——FHQ Treap
    提升市场调研和竞品分析效率:利用Appium实现App数据爬取
    Java进阶——如何查看Java字节码
    让你的Nginx支持分布式追踪opentracing
    基于51单片机和DHT11温湿度传感器的智能加湿器的设计与实现
    java基于SpringBoot+Vue+nodejs的在线外卖订餐系统Element
    【Arduino+ESP32专题】串口的简单使用
    Centos8系统配置Redis实现开机自启
    【python】numpy库
    2022/11/24 [指针] 用函数调用实现字符串的复制(字符型指针)
  • 原文地址:https://blog.csdn.net/sgliquangang/article/details/138182877