求助:NSMutableArray中修改单个字典值导致所有字典值同步变更
Hey there, I’ve run into this exact issue before—let’s break down what’s happening and how to fix it quickly.
The Root Cause
The problem you’re seeing boils down to one key detail: you’re adding the same single NSMutableDictionary object to your array multiple times. In Objective-C, collections like NSMutableArray store references to objects, not copies of them. So every entry in your array is just a pointer pointing to the same underlying dictionary. When you modify one entry, you’re actually editing that single shared dictionary—which is why all entries show the same change.
Here’s what your problematic code likely looks like:
// ❌ Mistake: Reusing the same dictionary object NSMutableDictionary *sharedDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"QTY_Value", nil]; NSMutableArray *itemArray = [NSMutableArray array]; // Add the SAME dictionary to the array multiple times for (int i = 0; i < 3; i++) { [itemArray addObject:sharedDict]; } // Modify the first "entry" [[itemArray firstObject] setObject:@"5" forKey:@"QTY_Value"]; // Now every dictionary in itemArray has QTY_Value = 5—they're all the same object!
The Fix: Ensure Each Dictionary Is a Unique Object
You need to make sure every entry in your array is a separate NSMutableDictionary instance. There are two straightforward ways to do this:
1. Create a New Dictionary for Each Entry
Instead of reusing one dictionary, initialize a new one every time you add to the array:
// ✅ Correct: Create a new dictionary each time NSMutableArray *itemArray = [NSMutableArray array]; for (int i = 0; i < 3; i++) { NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"QTY_Value", nil]; [itemArray addObject:newDict]; } // Modify the first entry [[itemArray firstObject] setObject:@"5" forKey:@"QTY_Value"]; // Now only the first dictionary has QTY_Value = 5—others stay at 1
2. Copy the Template Dictionary (For Reusable Templates)
If you have a template dictionary you want to base each entry on, use mutableCopy to create a new independent dictionary each time:
// ✅ Correct: Use mutableCopy to create unique instances NSMutableDictionary *templateDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"QTY_Value", nil]; NSMutableArray *itemArray = [NSMutableArray array]; for (int i = 0; i < 3; i++) { // mutableCopy creates a new, separate mutable dictionary NSMutableDictionary *newDict = [templateDict mutableCopy]; [itemArray addObject:newDict]; } // Modify the first entry [[itemArray firstObject] setObject:@"5" forKey:@"QTY_Value"]; // Again, only the first dictionary updates
Bonus: Deep Copy for Nested Mutable Objects
If your dictionary contains other mutable objects (like nested NSMutableArrays or NSMutableDictionaries), mutableCopy only does a shallow copy (it copies the top-level dictionary but references the same nested objects). For a full deep copy, use NSKeyedArchiver and NSKeyedUnarchiver:
// Deep copy example for nested mutable objects NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:templateDict requiringSecureCoding:NO error:nil]; NSMutableDictionary *deepCopiedDict = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSMutableDictionary class] fromData:archiveData error:nil];
Key Takeaway
Always double-check that each object you add to your array is a unique instance. Collections store references, not copies—so reusing the same object across entries will lead to unexpected synchronized changes like you saw.
内容的提问来源于stack exchange,提问作者kiran

