Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace class_createInstance in arc?

I have this code and need to port it to arc but I dont know how:

        case FIELDTYPE_OBJECT:
            className = [fieldType substringWithRange:NSMakeRange(2, [fieldType length]-3)];
            rel =  class_createInstance(NSClassFromString(className), sizeof(unsigned));
            Class theClass = [rel class];

            if ([rel isKindOfClass:[DbObject class]]) {
                //Load the record...
                NSInteger Id = [rs intForColumn:[theClass relationName]];
                if (Id==0) {
                    fieldValue = [rel init];
                } else {                    
                    Db *db = [Db currentDb];

                    fieldValue = [db loadById: theClass theId:Id];
                }
            }
            break;

The error is:

error: 'class_createInstance' is unavailable: not available in automatic reference counting mode

How replace it?

I need to build class objects in runtime.

like image 465
user1062923 Avatar asked Nov 23 '11 23:11

user1062923


1 Answers

The most straightforward solution is to add another file which has -fno-objc-arc set on it, and which has a function which calls class_createInstance() as above.

like image 133
Rob Rix Avatar answered Oct 13 '22 12:10

Rob Rix