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

相关推荐
二流小码农12 小时前
鸿蒙开发:DevEcoTesting中的稳定性测试
android·ios·harmonyos
库奇噜啦呼14 小时前
push [特殊字符] present
macos·ios·cocoa
胖虎114 小时前
iOS 16 SwiftUI 优雅跳转实践:用枚举路由和 NavigationStack 实现多页面导航
ios·swiftui·swift·swiftui跳转·navigationstack
软***c14 小时前
iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版分享
ios·iphone·iphone 解锁
Digitally14 小时前
如何将联系人从 iPhone 转移到 Android
android·ios·iphone
FreeBuf_15 小时前
黑客利用iMessage零点击漏洞攻击iPhone用户
ios·iphone
安和昂15 小时前
【iOS】多线程NSOperation,NSOperationQueue
macos·ios·cocoa
pop_xiaoli1 天前
OC—UI学习-2
学习·ui·ios
90后的晨仔1 天前
git 命令汇总
ios
liucan2332 天前
JS执行速度似乎并不比Swift或者C语言慢
前端·ios