I've looking for a way to get a property name as StringValue from inside a method.
Lets say:
My class has X Subviews from the Type UILabel.
@property (strong, nonatomic) UILabel *firstLabel;
@property (strong, nonatomic) UILabel *secondLabel;
[...]
and so on.
Inside the method foo, the views are iterated as followed:
-(void) foo
{
for (UIView *view in self.subviews) {
if( [view isKindOfClass:[UILabel class]] ) {
/*
codeblock that gets the property name.
*/
}
}
}
The Result should be something like that:
THE propertyName(NSString) OF view(UILabel) IS "firstLabel"
I've tried class_getInstanceVariable, object_getIvar and property_getName without Success.
For example, the code for:
[...]
property_getName((void*)&view)
[...]
Returns:
<UILabel: 0x6b768c0; frame = (65 375; 219 21); text = 'Something'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6b76930>>
But i'm looking for this kind of result: "firstLabel" , "secondLabel" and so on.
Solved
As in the Reply of graver described the solution is: class_copyIvarList which returns the name of the Ivars.
Ivar* ivars = class_copyIvarList(clazz, &count);
NSMutableArray* ivarArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
const char* ivarName = ivar_getName(ivars[i]);
[ivarArray addObject:[NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding]];
}
free(ivars);
See posts: https://stackoverflow.com/a/2302808/1228534 and Objective C Introspection/Reflection