Create objective-c class instance by name?

前端 未结 4 1496
深忆病人
深忆病人 2020-11-28 02:30

Is it possible to create an instance of a class by name? Something like:

NSString* className = @\"Car\";
id* p = [Magic createClassByName:className];
[p turn         


        
相关标签:
4条回答
  • 2020-11-28 03:11
    id object = [[NSClassFromString(@"NameofClass") alloc] init];
    
    0 讨论(0)
  • 2020-11-28 03:14

    NSClassFromString() runs the risk of mistyping the class name or otherwise using a class that doesn't exist. You won't find out until runtime if you make that error. Instead, if you use the built-in objective-c type of Class to create a variable, then the compiler will verify that the class exists.

    For example, in your .h:

    @property Class NameOfClass;
    

    and then in your .m:

    id object = [[NameOfClass alloc] init];
    

    If you mistyped the class name or if it doesn't exist, you'll get an error at compile time. Also I think this is cleaner code.

    0 讨论(0)
  • 2020-11-28 03:15

    If you are working with Objective-C without the NeXTstep (OS X, iOS, GNUstep etc) system or you just think this method is cleaner, then you could utilize the Objective-C language runtime library's API. Under Objective-C 2.0:

    #import <objc/runtime.h>
    //Declaration in the above named file
    id objc_getClass(const char* name);
    //Usage
    id c = objc_getClass("Object");
    [ [ c alloc ] free ];
    

    Under the Objective-C (1.0 or unnamed version) you would utilize the following:

    #import <objc/objc-api.h>
    //Declaration within the above named file
    Class objc_get_class( const char* name);
    //Usage
    Class cls = objc_get_class( "Test" );
    id obj = class_create_instance( cls );
    [ obj free ];
    

    I haven't tested the 1.0 version, however I have used the 2.0 function in code that is now in production. I personally believe utilizing the 2.0 function is cleaner if available than the NS function as it consumes less space: the length of the name in bytes + 1 ( null terminator ) for the 2.0 API versus the sum of two pointers (isa, cstring), a size_t length (cstring_length), and the length of the string in bytes + 1 for the NeXTSTEP API.

    0 讨论(0)
  • 2020-11-28 03:20
    @interface Magic : NSObject
    + (id)createInstanceOfClass:(Class)classe;
    @end
    
    @implementation Magic
    
    + (id)createInstanceOfClass:(Class)classe
    {
        return [[classe alloc] init];
    }
    
    @end
    

    Then to use it:

    Car *car = [Magic createInstanceOfClass:[Car class]];
    [car engineTurnOn];
    
    0 讨论(0)
提交回复
热议问题