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]
OK -- there appears to be no need to do any funky runtime gymnastics to solve your problem (don't get me wrong -- funky runtime gymnastics can be fun.... but it is probably just a waste of time here).
To restate the question:
You have a single class (MyClass) with many instances of that class. The class provides several attributes with instances of the class having many different values for that attribute. You have a collection of instances and you want to easily grab a collection of multiple subsets where the objects in each subset have the same value for a particular attribute.
To that end, I'd just loop over the original question and do something like (this code has never seen a compiler; it'll probably not compile):
// implement this on MyClass
+ (NSDictionary*)subdivideArrayOfMyClass:(NSArray*) aCollection byAttribute:(NSString*) anAttribute
{
NSMutableDictionary *collector = [NSMutableDictionary dictionary];
for (MyClass *anObject in aCollection) {
id value = [anObject valueForKey: anAttribute];
// you'd have to special case for nil here if you wanted.
NSMutableArray *sameValueCollector = [collector objectForKey: value];
if (!sameValueCollector) {
sameValueCollector = [NSMutableArray array];
[collector setObject: sameValueCollector forKey:value];
}
[sameValueCollector addObject:anObject];
}
return collector; // no need to turn it into an immutable dict
}
This does assume that all the values are safe for use as keys in a dictionary. That would be true for the examples you gave.
If you need all objects with a single value, you could do valueForKey: on the array. If you wanted, you could do some funky set arithmetic to sub-divide the objects. However, that'll likely incur multiple passes over the collection whereas the above does the work with one pass (though a potentially large quantity of hash lookups in the collector). Go with the straightforward until performance analysis indicates that it is too slow.
Use valueForKey:
.
id object = [customObject valueForKey:[NSString stringWithUTF8String:propertyName]];
You can use isEqual:
to compare. You will get an NSNumber
or NSValue
where appropriate. Read the KVC guide for more.
Earlier Answer
Since the getter method will match the property name, you can generate the selector of the property name and call performSelector
on the object.
[customObject performSelector:NSSelectorFromString([NSString stringWithUTF8String:propertyName])];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With