Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24453

Is there any way to declare class dynamically on Objective-C?

I just think about this case, I want to declare a class C which extends from other classes dynamically. Suppose that I have class A and class B. Now, I want the class C extends A or B base on a specified condition such as:

#ifdef USE_CLASS_A
class C: A
#else 
Class C: B
#endif

How can I do this in Objective-C?

UPDATE: I just need dynamic at compile time, not runtime. I mean, during compiling time, the class C will extend A or B. This extending will be go forever, don't change at runtime.

Upvotes: 2

Views: 126

Answers (1)

CRD
CRD

Reputation: 53010

Based on your update the answer is yes - using #if/#ifdef you can determine at compile time whether class C extends class A or class B in exactly the way you have suggested.

Of course classes A & B better provide same set of common methods and your code only call those methods, or else you will need further conditional code (compile or runtime) at the points you use class C.

Upvotes: 1

Related Questions