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

相关推荐
叽哥3 小时前
Flutter Riverpod上手指南
android·flutter·ios
用户091 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan1 天前
iOS26适配指南之UIColor
ios·swift
权咚2 天前
阿权的开发经验小集
git·ios·xcode
用户092 天前
TipKit与CloudKit同步完全指南
ios·swift
法的空间2 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
Magnetic_h3 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview