Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-generating code to create an object with an unknown class [duplicate]

I'm coding a script in Python that reads from a custom text and generates Objective-C code from it. The structure of the text is as follows:

<XClassName> {
    property_name_1 {
        var_name_1: var_value_1
        var_name_2: var_value_2
        property_name_2 {
            var_name_3: var_value_3
        }
        property_name_2 {
            var_name_3: var_value_4
        }
    }
    property_name_3 {
    }
}

Resulting in Objective-C code like this:

XClassName* object = [[XClassName alloc] init];
object.propertyName1 = [[[object.propertyName1 class] alloc] init];
object.propertyName1.varName1 = varValue1;
object.propertyName1.varName2 = varValue2;
object.propertyName2Array = [NSMutableArray array];
{
    PropertyName2Class propertyName2 = [[PropertyName2Class alloc] init];
    propertyName2.varName3 = varValue3;
    [object.propertyName2Array addObject:propertyName2];
}
{
    PropertyName2Class propertyName2 = [[PropertyName2Class alloc] init];
    propertyName2.varName3 = varValue4;
    [object.propertyName2Array addObject:propertyName2];
}
object.propertyName3 = [[[object.propertyName3 class] alloc] init];

This is fine except that PropertyName2Class is not known during the script runtime. Right now I have to manually look for the class name for the elements expected for the object's arrays but this defeats the purpose of having a script automate it.

Is there a way to create an object dynamically without knowing its class name and assigning values to its properties? Something like:

id classObject = ...; // How to instantiate a dynamic unknown class?
classObject.property1 = 1;
classObject.property2 = @"Hello World!";

Any ideas?

like image 374
jglievano Avatar asked Feb 15 '23 12:02

jglievano


1 Answers

If you know the class you want to instantiate exists, for example if it is passed in as a variable, then that's relatively easy. You can do:

- (id)createInstanceOfClass: (NSString *)className
{
  Class aClass = NSClassFromString(className);
  id instance = nil
  if(nil != aClass)
  {
    instance = [[aClass alloc] init];
  }
  return instance;
}

If it doesn't already exist, you can create an instance of it inside the run time, but that's a little trickier (and quite a bit dodgier too).

Update

I'd avoid trying to use a class with the properties that you need that already exists unless you're certain what it's implementation does. A safer solution would be to build a class up from scratch and specify what the implementation is.

Try reading:

http://www.mikeash.com/pyblog/friday-qa-2010-11-6-creating-classes-at-runtime-in-objective-c.html

(Not for the faint hearted!)

Also see the Objective-C runtime reference:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008048

like image 52
David Doyle Avatar answered Mar 23 '23 00:03

David Doyle