Objective-C: How to get the value of the `objc_property_t` property?

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]

+6
source share
2 answers

OK - you don’t need to do any funky gymnastics to solve your problem (don’t get me wrong - funky gymnastics of the performance time can be fun ... but it is probably just a waste of time here).

To repeat the question:

You have one class (MyClass) with many instances of this class. A class provides several attributes with instances of a class that has many different values ​​for this attribute. You have a collection of instances, and you want to easily capture a collection of several subsets where the objects in each subset have the same value for a particular attribute.

To this end, I just go to the original question and do something like this (this code has never been seen by the compiler, it probably won't 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 assumes that all values ​​are safe to use as keys in a dictionary. This would be true for the examples you gave.

If you need all objects with a single value, you can do valueForKey: in an array. If you wanted, you could do some funky arithmetic to separate objects. However, this is likely to lead to several passes through the collection, while the above work with a single pass (although a potentially large number of hash requests in the collector). Go straight until a performance analysis shows that it is too slow.

+5
source

Use valueForKey:

 id object = [customObject valueForKey:[NSString stringWithUTF8String:propertyName]]; 

You can use isEqual: for comparison. If necessary, you will receive an NSNumber or NSValue . Read the KVC manual for more details.

Previous answer

Since the getter method will match the property name, you can generate a property name selector and call performSelector on the object.

 [customObject performSelector:NSSelectorFromString([NSString stringWithUTF8String:propertyName])]; 

Strike>

+3
source

Source: https://habr.com/ru/post/893325/


All Articles