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 8625027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:40:47+00:00 2026-06-12T07:40:47+00:00

I’m writing a recursive method that basically traverses a NSManagedObject object and converts it

  • 0

I’m writing a recursive method that basically traverses a NSManagedObject object and converts it to a JSON dictionary. I have the bulk of this done, but I am running into an issue where the method goes into an infinite loop when it comes to an object’s inverse.

For example, let’s say the method starts off with an object that has a class of Job, and inside of that Job object there is a property called surveys. The surveys is an NSSet that contains multiple JobSurvey objects. Each JobSurvey object contains an inverse back to the original Job object and the property is called “Job”.

When I run this through my method it starts the infinite loop by going into the job and starts to process each property. Once the method gets to the surveys property, it’ll be called again to process each JobSurvey object as expected. The method then processes each property until it reaches the Job (inverse) object. At that time it’ll continue to process that object, and thus creates the infinite loop.

Any thoughts on how I can fix this? I’m trying to write this method without having to create custom object classes with object mapping as it needs to be able to be used with any type of object I pass into it. Below is the code I have thus far.

- (NSDictionary *)encodeObjectsForJSON:(id)object
{
    NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] init];

    // get the property list for the object
    NSDictionary *props = [VS_PropertyUtilities classPropsFor:[object class]];

    for (NSString *key in props) {

        // get the value for the property from the object
        id value = [object valueForKey:key];

        // if the value is just null, then set a NSNull object
        if (!value) {

            NSNull *nullObj = [[NSNull alloc] init];
            [returnDictionary setObject:nullObj forKey:key];

        // if the value is an array or set, then iterate through the array or set and call this method again to encode it's values
        } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSSet class]]) {

            NSMutableArray *retDicts = [[NSMutableArray alloc] init];

            // encode each member of the array to a JSON dictionary
            for (id val in value) {
                [retDicts addObject:[self encodeObjectsForJSON:val]];
            }

            // add to the return dictionary
            [returnDictionary setObject:retDicts forKey:key];

        // else if this is a foundation object, then set it to the dictionary
        } else if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSNull class]] || [value isKindOfClass:[NSDate class]]) {

            [returnDictionary setObject:value forKey:key];

        // else this must be a custom object, so call this method again with the value to try and encode it
        } else {

            NSDictionary *retDict = [self encodeObjectsForJSON:value ];
            [returnDictionary setObject:retDict forKey:key];
        }
    }

    return returnDictionary;
}
  • 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-12T07:40:48+00:00Added an answer on June 12, 2026 at 7:40 am

    This is what I did. It is similar to what you are trying to achieve.

    link to da codes

    It is a bit more explicit than i think you are looking for but I thought I’d suggest.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.