6

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]

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
  • What are you trying to do? Down this path generally lies madness (save for in a handful of outlying esoteric situations that are pretty much crazy town, but still possible). – bbum Jul 22 '11 at 02:53
  • Please see the updated description for the idea behind this. – Rad'Val Jul 22 '11 at 12:45
  • This related question has what you need: [object attribute list](http://stackoverflow.com/questions/754824/get-an-object-attributes-list-in-objective-c) – Oscar Gomez Jul 22 '11 at 01:08
  • `property_getName` returns the name of the property (eg for an object with a property like this `obj.prop` it will return "prop") – Rad'Val Jul 22 '11 at 01:15

2 Answers2

5

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.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Ok, so basically you can access `mary.age` like this: `[mary valueForKey: @"age"];`? That's awesome, didn't knew that... – Rad'Val Jul 22 '11 at 17:56
  • Yup; Do a search for **Key Value Coding**. – bbum Jul 22 '11 at 17:59
  • OK, I've tested the code and indeed it works with `NSObjects` but for custom objects I get `-[Person objectForKey:]: unrecognized selector sent to instance 0x5f41730` I'll do some more digging, worst case scenario I'll implement the `objectForKey` method in Person class myself. Anyway is better than runtime gymnastics :D – Rad'Val Jul 22 '11 at 18:23
  • Yup -- been there done exactly that... scratchscratchscratchDERP. :) – bbum Jul 22 '11 at 21:52
3

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])];

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • Cool. If you find `NSSelectorFromString([NSString stringWithUTF8String:propertyName])` verbose, you should be able to replace it with `sel_getUid(propertyName)`. – Deepak Danduprolu Jul 22 '11 at 01:36
  • what about `@property(getter=boogerHead) BOOL isNasty;`? – bbum Jul 22 '11 at 02:52
  • @mindnoise This is far from being a proper answer. You might want to unmark it as the answer. The problem is clearly indicated indicated in the example above. @bbum You are right. I overlooked the `getter` part but even if we figure it out using the attributes provided by `property_getAttributes` function it is not as trivial as using `performSelector` which is not the right for primitives i.e. `BOOL`. We will have to make the proper calls based on the return type. Do you know of a better way to approach this? – Deepak Danduprolu Jul 22 '11 at 03:36
  • I see your point bbum, however, i can control the object's properties and the way they are declared and will avoid that situation. – Rad'Val Jul 22 '11 at 12:13
  • @deepak this serves well my purpose (the properties are never primitives and I don't use the 'getter' part) However, i'll uncheck the answer as being correct to see if someone can come up with a better way to do it. Personally I prefer to do it this way if I can control the object's property, it's simpler than parsing 'property_getAttributes' – Rad'Val Jul 22 '11 at 12:21
  • @mindnoise Please do. However, I would also like to know what is the idea behind doing this. Probably there is a better approach there. – Deepak Danduprolu Jul 22 '11 at 12:26