How can I replace class_createInstance in an arc?

I have this code and need to port it, but I do not 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; 

Error:

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

How to replace it?

I need to create class objects at runtime.

+6
source share
3 answers

The easiest solution is to add another file on which -fno-objc-arc is installed, and which has a function that calls class_createInstance (), as described above.

+3
source

Try the following:

 #include <objc/objc-runtime.h> id object = [[NSClassFromString(@"TheClassName") alloc] init]; 
+1
source

Create split .h / .c files and put something like this.

 id const MyCreateInstanceOfClass(Class const class) { id instance = class_createInstance(class, 0); return instance; } 

#include .h and call it. No need to set the -fno-bjc-arc switch for each file.

+1
source

Source: https://habr.com/ru/post/902261/


All Articles