I use the following code to repeat all the properties of an object. I successfully retrieve the property name as char , but I do not know how to get the property value , which is of type id . Any ideas on how I can 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: Thanks to everyone for the great comments and answers. Some of you have asked why I need this. Well, here is the reason:
I have several objects of the same class. Let them say that 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 goal is to find a general way to group objects based on a given Eg property name. 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]
source share