如何动态子类化一个类并在Objective C中即时创建子类?

时间:2023-01-15 15:13:09

I want to dynamically subclass a class (say NSString) and instantiate that subclass for testing purposes. How can I do that in Objective C?

我想动态子类化一个类(比如NSString)并实例化该子类以进行测试。我怎么能在Objective C中做到这一点?

1 个解决方案

#1


I suspect that you don't need to do what you're seeking to do, but without more specifics it's impossible to know for sure. Because Objective-C is a late-bound language, subclassing for testing purposes is rarely required. Instead, take a look at class categories or consider a redesign so that you can pass a test double (either stub, fake or mock) via a parameter of type id or conforming to a protocol.

我怀疑你不需要做你想要做的事情,但没有更多具体细节,我们无法确定。因为Objective-C是一种后期绑定语言,所以很少需要用于测试目的的子类。相反,请查看类类别或考虑重新设计,以便您可以通过类型为id的参数或符合协议传递测试double(stub,false或mock)。

For interest, you can use objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes) to allocate a new class. You can add methods using class_addMethod(Class cls, SEL name, IMP imp, const char *types) and ivars with class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types). Finally, you should register the new class using objc_registerClassPair(Class cls). Find out more in the Objective-C 2.0 Runtime Reference. If I find time, I'll test some code and post it here.

有兴趣的话,您可以使用objc_allocateClassPair(Class超类,const char * name,size_t extraBytes)来分配新类。您可以使用class_addMethod(Class cls,SEL name,IMP imp,const char * types)和ivars with class_addIvar(Class cls,const char * name,size_t size,uint8_t alignment,const char * types)添加方法。最后,您应该使用objc_registerClassPair(Class cls)注册新类。在Objective-C 2.0运行时参考中了解更多信息。如果我找时间,我会测试一些代码并在此处发布。

#1


I suspect that you don't need to do what you're seeking to do, but without more specifics it's impossible to know for sure. Because Objective-C is a late-bound language, subclassing for testing purposes is rarely required. Instead, take a look at class categories or consider a redesign so that you can pass a test double (either stub, fake or mock) via a parameter of type id or conforming to a protocol.

我怀疑你不需要做你想要做的事情,但没有更多具体细节,我们无法确定。因为Objective-C是一种后期绑定语言,所以很少需要用于测试目的的子类。相反,请查看类类别或考虑重新设计,以便您可以通过类型为id的参数或符合协议传递测试double(stub,false或mock)。

For interest, you can use objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes) to allocate a new class. You can add methods using class_addMethod(Class cls, SEL name, IMP imp, const char *types) and ivars with class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types). Finally, you should register the new class using objc_registerClassPair(Class cls). Find out more in the Objective-C 2.0 Runtime Reference. If I find time, I'll test some code and post it here.

有兴趣的话,您可以使用objc_allocateClassPair(Class超类,const char * name,size_t extraBytes)来分配新类。您可以使用class_addMethod(Class cls,SEL name,IMP imp,const char * types)和ivars with class_addIvar(Class cls,const char * name,size_t size,uint8_t alignment,const char * types)添加方法。最后,您应该使用objc_registerClassPair(Class cls)注册新类。在Objective-C 2.0运行时参考中了解更多信息。如果我找时间,我会测试一些代码并在此处发布。