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");

相关推荐
QuantumLeap丶4 小时前
《Flutter全栈开发实战指南:从零到高级》- 09 -常用UI组件库实战
flutter·ios·dart
2501_915918415 小时前
App 上架苹果商店全流程详解 从开发者账号申请到开心上架(Appuploader)跨平台免 Mac 上传实战指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
2501_916007476 小时前
从零开始学习iOS App开发:Xcode、Swift和发布到App Store完整教程
android·学习·ios·小程序·uni-app·iphone·xcode
Pluto53810 小时前
第一个app产品的迭代
ios·github
2501_9159214311 小时前
iOS 26 CPU 使用率监控策略 多工具协同构建性能探索体系
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹11 小时前
JAVA国际版同城打车源码同城服务线下结账系统源码适配PAD支持Android+IOS+H5
android·java·ios·小程序·交友
游戏开发爱好者811 小时前
iOS 应用逆向对抗手段,多工具组合实战(iOS 逆向防护/IPA 混淆/无源码加固/Ipa Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张12 小时前
iOS 文件管理与导出实战,多工具协同打造高效数据访问与调试体系
android·macos·ios·小程序·uni-app·cocoa·iphone
AirDroid_cn19 小时前
在 iOS 18 中,如何将锁屏底部的 “手电筒” 替换为其他应用?
ios
芝麻开门-新起点1 天前
Android 和 iOS 系统版本及开发适配
android·ios·cocoa