1

Is it possible to create classes dynamically in Objective-C?

I want to do something like this, but dynamically:

Mission *mission = [[Mission alloc]init];
        mission.Id = [modelObject Id];
        mission.EntityState = [modelObject EntityState];
        mission.Description = [modelObject Description]; 

So I tried this

id clonedobject = [[[modelObject class] alloc]init];
for (NSString *key in dic) 
    {
        [[clonedobject valueForKey:[dic objectForKey:key]] addObject:[modelObject valueForKey:[dic objectForKey:key]]];
    }

But with this code, I cannot reach for example the id with clonedobject.Id. It says "Property ID not found on object of type id.

Is there a way to do something like this?

[modelObject class] *clonedobject = [[[modelObject class] alloc]init]
Monolo
  • 18,205
  • 17
  • 69
  • 103
DeFlo
  • 141
  • 1
  • 1
  • 9

2 Answers2

6

please refer to the given link .. Creating class at runtime

Ali3n
  • 1,244
  • 6
  • 10
  • +1 for the link. But you should consider adding the essence of that article over here. (Just for a quick reference) – viral Apr 25 '13 at 12:11
0

It is possible to register classes at runtime, but this is not what you're looking for.

Even if you could add classes at runtime, it would not help you with the compile-time type of the variable. i.e. You would still not know the type at compile-time, and thus you still cannot use properties using dot notation like clonedobject.id.

How do you know that this object has a property called id? It seems like you already know what type it is.

newacct
  • 119,665
  • 29
  • 163
  • 224