iOS runtime

---参考文章---

  • 暂时没有

一、如何在Xcode中使用runtime

Xcode默认是不建议开发者使用runtime的,所以在Xcode直接使用runtime的语法是会报错误的。

如果要在Xcode中使用runtime的语法,是需要配置一下才可以使用,配置方法如下图:

  • 首先在1的位置搜索Enable strict
  • 默认是选中Yes的,然后只要选中No即可,然后在项目中使用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];

    }

  • 获取当前对象的所有属性

    /* 获取对象的所有属性 */

    • (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;
      }

  • 调用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");

相关推荐
namehu9 小时前
React Native 应用性能分析与优化不完全指南
android·react native·ios
游戏开发爱好者815 小时前
BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088915 小时前
iOS 26 系统流畅度实战指南|流畅体验检测|滑动顺畅对比
android·macos·ios·小程序·uni-app·cocoa·iphone
Digitally16 小时前
如何处理旧 iPhone:安全地回收或重新利用
安全·ios·iphone
岁月向前16 小时前
iOS蓝牙常见问题
ios
一大树16 小时前
H5在不同操作系统与浏览器中的兼容性挑战及全面解决方案
前端·ios
2501_9151063217 小时前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214318 小时前
iOS 26 崩溃日志解析,新版系统下崩溃获取与诊断策略
android·ios·小程序·uni-app·cocoa·iphone·策略模式
林鸿群19 小时前
Apple M3 MacOS arm64 编译QGroundControl5.0.8(base on Qt 6.8.3)
macos·ios·qgc·qgroundcontrol