realloc() & ARC

Posted by RynoB on Stack Overflow See other posts from Stack Overflow or by RynoB
Published on 2012-07-11T14:15:03Z Indexed on 2012/07/11 15:15 UTC
Read the original article Hit count: 430

How would I be able to rewrite the the following utility class to get all the class string values for a specific type - using the objective-c runtime functions as shown below?

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

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

"Implicit conversion of a non-Objective-C pointer type 'void *' to '__unsafe_unretained Class *' is disallowed with ARC"

The the below code is a reference 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 will be much appreciated.

Thanks

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about automatic-ref-counting