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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:36:00+00:00 2026-05-17T22:36:00+00:00

Edit: better example… So I have an NSMutableSet of existing objects and I’d like

  • 0

Edit: better example…

So I have an NSMutableSet of “existing” objects and I’d like to download JSON data, parse it, and merge these new objects with my existing ones, updating any duplicates with the newly downloaded ones. Here’s what the existing set of objects looks like:


NSArray *savedObjects = [NSArray arrayWithObjects:
    [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", @"hello there", @"body", @"200", @"score", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:@"2", @"id", @"hey now", @"body", @"10", @"score", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:@"3", @"id", @"welcome!", @"body", @"123", @"score", nil],
    nil
];
self.objects = [NSMutableSet setWithArray:savedObjects];

// after downloading and parsing JSON... I have an example array of objects like this:

NSArray *newObjects = [NSArray arrayWithObjects:
    [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", @"hello there", @"body", @"9999", @"score", nil],
    [NSDictionary dictionaryWithObjectsAndKeys:@"4", @"id", @"what's new", @"body", @"22", @"score", nil],
    nil
];

So for example, after merging this new object, the object with id 1 would now have a score of 9999 and a new object with id 4 would be added to the set.

I really want to avoid looping through the NSMutableSet for every new object just to check that the @”id” property exists… I was thinking I could use addObjectsFromArray to merge these new objects but it looks like since the properties differ (eg: score: 9999) it’s not seeing the new objects as an existing object in the set.

I’m using the numbers as strings to simplify this example. I’d also like to avoid using iOS SDK 4.0-only features since the app will be 3.0-compatible.

Thanks a ton! I appreciate it!

  • 1 1 Answer
  • 1 View
  • 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-17T22:36:01+00:00Added an answer on May 17, 2026 at 10:36 pm

    Your best bet is to convert the first set into a dictionary using the @”id” parameter. Here’s some code that should do what you want, and it will even handle the case of having dictionaries that have no @”id” parameter (at which point they won’t be merged, just included in the result):

    // here is the NSSet you already have
    self.objects = [NSSet setWithObjects:
        [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", @"hello there", @"body", @"200", @"score", nil],
        [NSDictionary dictionaryWithObjectsAndKeys:@"2", @"id", @"hey now", @"body", @"10", @"score", nil],
        [NSDictionary dictionaryWithObjectsAndKeys:@"3", @"id", @"welcome!", @"body", @"123", @"score", nil],
        nil];
    
    // after downloading and parsing JSON... I have an example array of objects like this:
    
    NSArray *newObjects = [NSArray arrayWithObjects:
        [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", @"hello there", @"body", @"9999", @"score", nil],
        [NSDictionary dictionaryWithObjectsAndKeys:@"4", @"id", @"what's new", @"body", @"22", @"score", nil],
        nil];
    
    // Convert self.objects into a dictionary for merging purposes
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:[self.objects count]];
    NSMutableSet *remainder = [NSMutableSet set]; // for objects with no @"id"
    for (NSDictionary *obj in self.objects) {
        NSString *objId = [obj objectForKey:@"id"];
        if (objId) {
            [dict setObject:obj forKey:objId];
        } else {
            [remainder addObject:obj];
        }
    }
    
    // merge the new objects in
    for (NSDictionary *obj in newObjects) {
        NSString *objId = [obj objectForKey:@"id"];
        if (objId) {
            // merge the new dict with the old
            // if you don't want to merge the dict and just replace,
            // simply comment out the next few lines
            NSDictionary *oldObj = [dict objectForKey:objId];
            if (oldObj) {
                NSMutableDictionary *newObj = [NSMutableDictionary dictionaryWithDictionary:oldObj];
                [newObj addEntriesFromDictionary:obj];
                obj = newObj;
            }
            // stop commenting here if you don't want the merging
            [dict setObject:newObj forKey:objId];
        } else {
            [remainder addObject:obj];
        }
    }
    
    // add the merged dicts back to the set
    [remainder addObjectsFromArray:[dict allValues]];
    self.objects = remainder;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem trying to edit. I work with Areas for better management
Edit: Translated I have a RSS-feed that i want to parse. It's a podcast
EDIT: I have a table with 3 rows like so. ID NAME REV 1
Jump to the EDIT for a better explanation! I have tried to get this
I'm looking to analyze and compare the following `signals': (Edit: better renderings here: oscillations
EDIT: I'd better rephrase: How can I shift the GET-implementation of a Class property
EDIT: Is there a better way of doing this ? TPendingBets = class(TDataModule) private
EDIT I made a screen shot to show better what I'm attempting to do,
this is an edit of the original post now that I better understand the
-EDIT- <li> would probably be better but the same question still applies. So I

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.