Ios/objective-c Development Dictionary nsdictionary deep Copy (use category)

Source: Internet
Author: User

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   
    1. #import <Foundation/Foundation.h>
    2. @interface Nsdictionary (mutabledeepcopy)
    3. -(Nsmutabledictionary *) mutabledeepcopy;
    4. Add Mutabledeepcopy method
    5. @end


NSDICTIONARY+MUTABLEDEEPCOPY.M:

C + + code
  1. #import "Nsdictionary+mutabledeepcopy.h"
  2. @implementation Nsdictionary (mutabledeepcopy)
  3. -(Nsmutabledictionary *) mutabledeepcopy
  4. {
  5. Nsmutabledictionary *dict=[[nsmutabledictionary Alloc] initwithcapacity:[self Count]];
  6. //Create a new Nsmutabledictionary object, size is the size of the original Nsdictionary object
  7. Nsarray *keys=[self AllKeys];
  8. For (ID key in keys)
  9. {//loop read copy each element
  10. ID value=[self Objectforkey:key];
  11. ID copyvalue;
  12. if ([Value respondstoselector: @selector (mutabledeepcopy)]) {
  13. //If the element corresponding to the key can respond to the Mutabledeepcopy method (or Nsdictionary), call the Mutabledeepcopy method to copy
  14. Copyvalue=[value Mutabledeepcopy];
  15. }Else if ([Value respondstoselector: @selector (mutablecopy)])
  16. {
  17. Copyvalue=[value Mutablecopy];
  18. }
  19. if (copyvalue==nil)
  20. Copyvalue=[value copy];
  21. [Dict Setobject:copyvalue Forkey:key];
  22. }
  23. return dict;
  24. }
  25. @end

Test:

C + + code
  1. #import <Foundation/Foundation.h>
  2. #import "Nsdictionary+mutabledeepcopy.h"
  3. Import Header File
  4. int main (int argc, const char * argv[])
  5. {
  6. @autoreleasepool {
  7. Nsmutablearray *arr1=[[nsmutablearray alloc] initwithobjects:@"AA", @"BB", @"CC", nil];
  8. Nsdictionary *dict1=[[nsdictionary alloc] initwithobjectsandkeys:arr1,@"arr1", nil];
  9. NSLog (@"%@", Dict1);
  10. Nsmutabledictionary *dict2=[dict1 Mutablecopy];
  11. //Shallow copy
  12. Nsmutabledictionary *dict3=[dict1 Mutabledeepcopy];
  13. //Deep copy
  14. [arr1 addobject:@"DD"];
  15. NSLog (@"%@", dict2);
  16. NSLog (@"%@", dict3);
  17. }
  18. return 0;
  19. }

Ios/objective-c Development Dictionary nsdictionary deep Copy (use category)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.