Realloc () & ARC

How can I rewrite the following utility class to get all the string values โ€‹โ€‹of a class for a particular type - using the Objective-c runtime functions as shown below?

The ARC documentation states that realloc should be avoided, and I also get the following compiler error on this line:

classList = realloc(classList, sizeof(Class) * numClasses); 

"Implicit conversion of non-Objective-C pointer type 'void *' to '__unsafe_unretained Class *' is not allowed using ARC

Below is the code for the link to the original article, which can be found here .

 + (NSArray *)classStringsForClassesOfType:(Class)filterType { int numClasses = 0, newNumClasses = objc_getClassList(NULL, 0); Class *classList = NULL; while (numClasses < newNumClasses) { numClasses = newNumClasses; classList = realloc(classList, sizeof(Class) * numClasses); newNumClasses = objc_getClassList(classList, numClasses); } NSMutableArray *classesArray = [NSMutableArray array]; for (int i = 0; i < numClasses; i++) { Class superClass = classList[i]; do { superClass = class_getSuperclass(superClass); if (superClass == filterType) { [classesArray addObject:NSStringFromClass(classList[i])]; break; } } while (superClass); } free(classList); return classesArray; } 

Your help would be greatly appreciated.

+7
source share
1 answer

ARC makes you more explicit with your memory management. In this case, you just need to explicitly display the realloc output:

 classList = (Class *)realloc(classList, sizeof(Class) * numClasses); 
+13
source

All Articles