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

  • Home
  • SEARCH
  • 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 8065647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:44:04+00:00 2026-06-05T11:44:04+00:00

I am trying to deep-parse my own collection of objects into an NSDictionary (for

  • 0

I am trying to deep-parse my own collection of objects into an NSDictionary (for JSON).

I have a base object class, which All of my models extend, and this baseobject in turn extends NSObject:

@interface BaseObj : NSObject <DICTMaker>
    - (NSMutableDictionary *) toDICT;
@end

In the method, I use objc-runtime to get a list of properties. Any property which I can get a reference to using [self valueForKey:], I go ahead and insert into my dictionary the name and value of the property.

However, what I’ve noticed thus far is that NSNumbers and user defined classes are not added to the dictionary! My parser most assuredly identifies them, because I have it spitting out everything to the log; but [self valueForKey:] returns nil on all NSNumbers and user defined objects.

- (NSMutableDictionary *)toDICT {
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];

        // Both of these work, I promise:
        NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
        NSString *propertyType = [NSString stringWithUTF8String:getPropertyType(property)];

        NSLog( @"%@ of Type: %@", propertyName, propertyType );
        id propertyValue = [self valueForKey:propertyName];
        if ( [ propertyValue respondsToSelector:@selector(toDICT:) ] )
            [ props setObject:[propertyValue toDICT] forKey:propertyName ];
        else if ( propertyValue )
            [ props setObject:propertyValue forKey:propertyName ];
        else
            NSLog( @"Unable to get ref to: %@", propertyName );
    }
    free(properties);
    return props;
}

Here is a sample object I threw at the creator:

@interface UserRegistrationLocation : BaseObj {
    NSString *address, *street, *street_2, *city;
    NSNumber *addr_state, *addr_zip;
}

@interface UserRegistrationContact : BaseObj {
    NSString *first_name, *last_name;
    NSString *p_phone_area, *p_phone_first_3, *p_phone_last_4;
    NSString *s_phone_area, *s_phone_first_3, *s_phone_last_4;
}

@interface UserRegistration : BaseObj {
    NSString *email, *password, *password_confirm;
    NSNumber *referral;

    UserRegistrationContact *primary, *secondary;
    UserRegistrationLocation *address;
}

NSMutableDictionary *mydict = [myUserRegistration toDICT];

The resulting dictionary only contained entries for email, password, and password_confirm:

[11012:f803] Unable to get ref to: referral
[11012:f803] Unable to get ref to: primary
[11012:f803] Unable to get ref to: secondary
[11012:f803] Unable to get ref to: address
[11012:f803] {"user":{"password":"haxme123","password_confirm":"haxme123","email":"my@email.com"}}

Any assistance please =} !

  • 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-06-05T11:44:05+00:00Added an answer on June 5, 2026 at 11:44 am

    Maybe I understood the problem wrong, but are your referral, primary and so not simply null?
    If they are not in the keyvalue store, you would get an exception.
    You else part is only called, if the property is found in the keyvalue store but has a nil assigned. At least from your example code, one cannot decide, if the values are set.

    If I reduce your example to the code below, I get the following output
    test with Value: (null)
    Unable to get ref to: test
    aNumber with Value: 5

    test is null and will give your message “unable…”. aNumber is correct. If I change test to some text, the “unable…” part vanishes. The additional member variable _noProp which is not a property does not occur here, as copyPropertyList copies only properties.

    @interface TestValueForKey : NSObject {
         NSString* _test;
         NSString* _noProp;
         NSNumber* _aNumber;
    }
    
    @property (retain) NSString* test;
    @property (retain) NSNumber* aNumber;
    
    -(void)myTest;
    
    @implementation TestValueForKey
    
    @synthesize test = _test;
    @synthesize aNumber = _aNumber;
    
    -(id)init
    {
        if( (self = [super init]) != nil) {
           _test = nil;
           _aNumber = [NSNumber numberWithInt:5];
        }
    
        return self;
    }
    
    -(void)myTest
    {
        NSMutableDictionary *props = [NSMutableDictionary dictionary];
        unsigned int outCount, i;
        objc_property_t *properties = class_copyPropertyList([self class], &outCount);
        for( i = 0; i < outCount; i++) {
           objc_property_t property = properties[i];
    
           // Both of these work, I promise:
           NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
    
          id propertyValue = [self valueForKey:propertyName];
          NSLog( @"%@ with Value: %@", propertyName, propertyValue );
          if ( propertyValue )
              [ props setObject:propertyValue forKey:propertyName ];
          else
              NSLog( @"Unable to get ref to: %@", propertyName );
      }
      free(properties);
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to return as JSON the fully deep object (with all of
I'm currently trying to dig deep into python and I have found a challenge
I have a rather deep hierarchy of objects that I'm trying to persist with
I have some code that performs a deep copy using Object.clone, but I'm trying
I'm trying to parse the JSON file of a Reddit thread with all it's
I'm trying to make a deep copy of an object, including a GregorianCalendar instance.
I am trying to get 4 List deep List collection, List<List<List<List<int>>>> . From my
I'm trying to deep-load a EF object and keep it in cache for a
I am trying to build an application which serves images to a Deep Zoom
I'm trying to go deep into Dictionary ADT and Skip List for Java. My

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.