Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:42:20+00:00 2026-05-23T23:42:20+00:00

I use the following code to iterate through all the properties of an object.

  • 0

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]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:42:21+00:00Added an answer on May 23, 2026 at 11:42 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects, and i use following code to get in
I have the following code. I want to use the methods for myserver1 and
This may well have come up before but the following code is taken from
I use following code: Create a retry policy, when error, retry after 1 second,
I use VS2010, C# to develop Silverlight 4 app, I use following code in
I use the following code try to create an array of string vectors, I
I use the following code to layout network drives on a system. I want
I use the following code: Calendar calendar = new GregorianCalendar(0,0,0); calendar.set(Calendar.YEAR, 1942); calendar.set(Calendar.MONTH, 3);
I use the following code to allocate a Console for a WinForm application. The
I use the following code: - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.