I use the following code to iterate through all the properties of an object. I successfully retrieve the property name
as char
but I have no idea how to get the property value
which is of id
type. Any ideas on how can I achieve this?
objc_property_t *allProperties = class_copyPropertyList([currentObject class], &allPropertyCount);
for (unsigned int i = 0; i < allPropertyCount; i++) {
objc_property_t property = allProperties[i];
const char * propertyName = property_getName(property);
}
========================================================================================
EDIT: Thank you all for the great comments and answers. Some of you asked why do I need this. Well, here is the reason:
I have several objects of the same class. Let's say the class is Person
and its instances are Mary, John and David. The properties of each object are set as follows:
mary.age = [NSNumber numberWithInt:20];
john.age = [NSNumber numberWithInt:45];
david.age = [NSNumber numberWithInt:20];
mary.gender = @"female";
john.gender = @"male";
david.gender = @"male";
My purpose is to find a generic way to group the objects based on a given property name Eg. this will create 2 groups [david and mary] and [john]:
[self groupBaseDataObjects:self.persons withPropertyName:"age"];
and this:
[self groupBaseDataObjects:self.persons withPropertyName:"gender"];
will also create 2 groups [john and david] and [mary]