runtime基本用法

1 当我们动态加载某个类的时候,可以用到runtime,可以便捷的获取到动态加载类的所有信息 (属性,属性名)

 /**
     *测试自动生成某个类
     */
    NSDictionary *dict =@{
                            @"class": @"WaitViewController",
                            @"property": @{
                            @"ID": @"123",
                            @"type": @"12"
                            }
    };
    NSString *class =[NSString stringWithFormat:@"%@",dict[@"class"]];
    const char *className =[class cStringUsingEncoding:NSUTF8StringEncoding];
    Class newClass =objc_getClass(className);
    if (!newClass) {
        Class superClass =[UIViewController class];
        newClass =objc_allocateClassPair(superClass, className, 0);
        objc_registerClassPair(newClass);
        
    }
    id instance =[[newClass alloc] init];
    NSDictionary *propertys =dict[@"property"];
    [propertys enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//加载类中的属性
        NSLog(@"%@",key);
        unsigned int outCount, i;
        objc_property_t *property1 =class_copyPropertyList([instance class], &outCount);
        for (i=0; i<outCount; i++) {
            objc_property_t property =property1[i];
            NSLog(@"%s",property_getAttributes(property));
//            NSLog(@"%@",method_getName((__bridge Method)(instance)));
            NSString *propertyName =[[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            if ([propertyName isEqualToString:key]) {
                NSLog(@"yes");
            }
        }
        
        
    }];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • 对于从事 iOS 开发人员来说,所有的人都会答出【runtime 是运行时】什么情况下用runtime?大部分人能...
    梦夜繁星阅读 3,734评论 7 64
  • 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的...
    西木阅读 30,643评论 33 466
  • 1.人很容易在忙碌和日复一日中失去目标,谁又能特立独行呢。 2.垃圾食品都特别好吃,人渣也特别有魅力,不过人渣终究...
    许小敬阅读 185评论 0 0
  • 夏天来了,又到了愉快吃冷饮、冰激凌的季节!不过,一代名医曾立下家规:家人后代什么都可以吃,就是不允许吃冷饮!这是为...
    付强执业兽医师阅读 1,175评论 0 0