Challenge Instance Variables list - solution


#1

My solution, obviously very easy last challenge. Only thing I am doubtful about is that in the reference for the class_copyIvarList() states it instance variables declared by the superclass are not included, so maybe I should have climbed the hiearchy as we did for the Hierarchy for classs function. Yet, doesn’t the cls argument we are passing when we call the helper function already includes ALL classes?

[code]// C Helper Funtion to list instance variables for each class
NSArray *ZGCiVarsForClass(Class cls) {
NSMutableArray *iVars = [NSMutableArray array];
unsigned int ivarCount = 0;
Ivar *iVarList = class_copyIvarList(cls, &ivarCount);
for (int v = 0; v < ivarCount; v++) {
// get current variable
Ivar currentIVar = iVarList[v];
// encode C string name for iVar into an NSString object
NSString *iVarName = [NSString stringWithCString:ivar_getName(currentIVar) // ivar_getName() function returns a C String.
encoding:NSUTF8StringEncoding];

    // add resulting NSString object to iVar array
    [iVars addObject];
    free(iVarList); // create-copy rule, must free these types of functions
    
}
return iVars; // return ivar array

}[/code]