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

相关推荐
奇客软件18 小时前
如何从相机的记忆棒(存储卡)中恢复丢失照片
深度学习·数码相机·ios·智能手机·电脑·笔记本电脑·iphone
GEEKVIP20 小时前
如何修复变砖的手机并恢复丢失的数据
macos·ios·智能手机·word·手机·笔记本电脑·iphone
一丝晨光1 天前
继承、Lambda、Objective-C和Swift
开发语言·macos·ios·objective-c·swift·继承·lambda
GEEKVIP1 天前
iPhone/iPad技巧:如何解锁锁定的 iPhone 或 iPad
windows·macos·ios·智能手机·笔记本电脑·iphone·ipad
KWMax2 天前
RxSwift系列(二)操作符
ios·swift·rxswift
Mamong2 天前
Swift并发笔记
开发语言·ios·swift
GEEKVIP2 天前
手机使用指南:如何在没有备份的情况下从 Android 设备恢复已删除的联系人
android·macos·ios·智能手机·手机·笔记本电脑·iphone
奇客软件2 天前
如何使用工具删除 iPhone 上的图片背景
windows·ios·智能手机·excel·音视频·cocoa·iphone
安和昂2 天前
【iOS】计算器的仿写
ios
SchneeDuan2 天前
iOS--App启动过程及优化
ios