美文网首页
oc动态创建类

oc动态创建类

作者: ADreamClusive | 来源:发表于2020-03-03 13:54 被阅读0次

import <objc/runtime.h>

- (void)createClass
{
    Class newClass = objc_allocateClassPair([NSString class], "NSStringSubclass", 0);
    class_addMethod(newClass, @selector(report), (IMP)ReportFunction, "v@:");
    objc_registerClassPair(newClass);
    id instanceOfNewClass = [[newClass alloc] init];
    [instanceOfNewClass performSelector:@selector(report) withObject:@"hello"];
}
void ReportFunction(id self, SEL _cmd, NSString *test) {
    NSLog(@"This object is %p.", self);
    NSLog(@"Class is %@, and super is %@.", [self class], [self superclass]);
    NSLog(@"ReportFunction:参数:%@",test);
}

相关文章

网友评论

      本文标题:oc动态创建类

      本文链接:https://www.haomeiwen.com/subject/rpmclhtx.html