Objective-c类运行时定义 [英] Objective-c Classes runtime definition

查看:122
本文介绍了Objective-c类运行时定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在objective-c中的运行时定义类?

例如我收到一个XML文件,它定义一个对象并创建它并在运行时使用它?

Is it possible to define classes at run time in objective-c?
E.g. I receive an XML file, that defines an object and create it and use it in run-time?

推荐答案

是的,检查一下代码片段,我在这里创建了一个类,只使用C方法和一个协议定义(为了简化消息调用)...

Yes, check this code snippet out, I created a class here, using only C methods, and one protocol definition (for simplicity when making message calls)...

MyCObject.h

MyCObject.h

#import <Foundation/Foundation.h>

@protocol MyCObj_methods<NSObject>

-(NSString *) getString;
-(int) getInt;
+(NSString *) someStaticMethod;

@end

typedef id<MyCObj_methods> MyCObj;
extern Class MyCObj_class;

__attribute__((constructor))
void MyCObj_initialize();

MyCObj MyCObj_alloc(        id self,     SEL _cmd );
MyCObj MyCObj_new(          id self,     SEL _cmd );

NSString *MyCObj_someStaticMethod ( id self, SEL _cmd );

MyCObj MyCObj_init(         MyCObj self, SEL _cmd );
NSString *MyCObj_getString( MyCObj self, SEL _cmd );
int MyCObj_getInt(          MyCObj self, SEL _cmd );

MyCObject.m

MyCObject.m

#import "MyCObject.h"
#import <objc/Object.h>
#import <objc/runtime.h>

static Class myStaticClass;
Class MyCObj_class;

typedef struct
{
    Class isa;
    NSString *myString;
    int myInt;
} MyCObj_t;

void MyCObj_initialize(void);

__attribute__((constructor))
void MyCObj_initialize()
{
    MyCObj_class = objc_allocateClassPair([NSObject class], "MyCObj", 0);
    objc_registerClassPair(MyCObj_class);

    myStaticClass = object_getClass(MyCObj_class);

    class_addMethod(MyCObj_class, @selector(init),      (IMP)MyCObj_init,        "@@:");
    class_addMethod(MyCObj_class, @selector(getString), (IMP)MyCObj_getString,   "@@:");
    class_addMethod(MyCObj_class, @selector(getInt),    (IMP)MyCObj_getInt,      "i@:");

    class_addMethod(myStaticClass, @selector(alloc),            (IMP)MyCObj_alloc, "@@:");
    class_addMethod(myStaticClass, @selector(new),              (IMP)MyCObj_new,   "@@:");
    class_addMethod(myStaticClass, @selector(someStaticMethod), (IMP)MyCObj_someStaticMethod, "@@:");
}

MyCObj MyCObj_alloc(id self, SEL _cmd)
{
    return (MyCObj) class_createInstance(MyCObj_class, sizeof(MyCObj_t) - sizeof(Class));
}

MyCObj MyCObj_new(id self, SEL _cmd)
{
    return (MyCObj) [[MyCObj_class alloc] init];
}

NSString *MyCObj_someStaticMethod(id self, SEL _cmd)
{
    return @"Some Static Method";
}

MyCObj MyCObj_init(MyCObj self, SEL _cmd)
{
    struct objc_super super = { .receiver = self, .super_class = [NSObject class] };

    if ((self = (MyCObj) objc_msgSendSuper(&super, _cmd)))
    {
        ((MyCObj_t *) self)->myString = @"hello world!";
        ((MyCObj_t *) self)->myInt = 15;
    }

    return self;
}

NSString *MyCObj_getString(MyCObj self, SEL _cmd)
{
    return ((MyCObj_t *) self)->myString;
}

int MyCObj_getInt(MyCObj self, SEL _cmd)
{
    return ((MyCObj_t *) self)->myInt;
}

用法:

MyCObj obj = [MyCObj_class new];
NSLog(@"%@ %i %@", [obj getString], [obj getInt], [MyCObj_class someStaticMethod]);

这篇关于Objective-c类运行时定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆