Objective: To convert a Nsdictionary object into a Nsmutabledictionary object whose content is an array of strings and requires full replication (deep copy).
If you call Nsdictionary's Mutablecopy method, you can get a Nsmutabledictionary object, but this is just shallow copy, if we modify the values in the Nsdictionary array (of course, Array must be Nsmutablearray), you will find that the value of the array within the Nsmutabledictionary object has changed. We need to add a Mutabledeepcopy method to implement a deep copy, in which each element is copied in a loop.
To achieve this function, there are two methods, one is inheritance, the other is the use of category. The difference between category and inheritance is that using category is not a new class, but rather an addition of methods on the basis of the original class (using the original class name), so that we do not need to modify the class name already written in the other source files, just import the H header file, Then change the copy method to our new method.
First, the new objective-c category file, I this category fill mutabledeepcopy,category on fill nsdictionary, so the resulting file is nsdictionary+ MutableDeepCopy.h and NSDICTIONARY+MUTABLEDEEPCOPY.M, the generated filenames are easy to understand.
Two, two file source code:
Nsdictionary+mutabledeepcopy.h
C + + code
- #import <Foundation/Foundation.h>
- @interface Nsdictionary (mutabledeepcopy)
- -(Nsmutabledictionary *) mutabledeepcopy;
- Add Mutabledeepcopy method
- @end
NSDICTIONARY+MUTABLEDEEPCOPY.M:
C + + code
- #import "Nsdictionary+mutabledeepcopy.h"
- @implementation Nsdictionary (mutabledeepcopy)
- -(Nsmutabledictionary *) mutabledeepcopy
- {
- Nsmutabledictionary *dict=[[nsmutabledictionary Alloc] initwithcapacity:[self Count]];
- //Create a new Nsmutabledictionary object, size is the size of the original Nsdictionary object
- Nsarray *keys=[self AllKeys];
- For (ID key in keys)
- {//loop read copy each element
- ID value=[self Objectforkey:key];
- ID copyvalue;
- if ([Value respondstoselector: @selector (mutabledeepcopy)]) {
- //If the element corresponding to the key can respond to the Mutabledeepcopy method (or Nsdictionary), call the Mutabledeepcopy method to copy
- Copyvalue=[value Mutabledeepcopy];
- }Else if ([Value respondstoselector: @selector (mutablecopy)])
- {
- Copyvalue=[value Mutablecopy];
- }
- if (copyvalue==nil)
- Copyvalue=[value copy];
- [Dict Setobject:copyvalue Forkey:key];
- }
- return dict;
- }
- @end
Test:
C + + code
- #import <Foundation/Foundation.h>
- #import "Nsdictionary+mutabledeepcopy.h"
- Import Header File
- int main (int argc, const char * argv[])
- {
- @autoreleasepool {
- Nsmutablearray *arr1=[[nsmutablearray alloc] initwithobjects:@"AA", @"BB", @"CC", nil];
- Nsdictionary *dict1=[[nsdictionary alloc] initwithobjectsandkeys:arr1,@"arr1", nil];
- NSLog (@"%@", Dict1);
- Nsmutabledictionary *dict2=[dict1 Mutablecopy];
- //Shallow copy
- Nsmutabledictionary *dict3=[dict1 Mutabledeepcopy];
- //Deep copy
- [arr1 addobject:@"DD"];
- NSLog (@"%@", dict2);
- NSLog (@"%@", dict3);
- }
- return 0;
- }
Ios/objective-c Development Dictionary nsdictionary deep Copy (use category)