IOS/Objective-C Development dictionary NSDictionary deep replication (use category), iosnsdictionary
Objective: to convert an NSDictionary object to an NSMutableDictionary object. The object content is a string array and must be fully copied (deep copy ).
If you call the mutableCopy method of NSDictionary, you can obtain an NSMutableDictionary object, but this is only a small copy. If we modify the value of the Number Group in NSDictionary (of course, the array must be NSMutableArray), we will find that, the value of the array in the NSMutableDictionary object is also changed. We need to add a mutableDeepCopy method to implement deep replication. In this method, each element is replicated cyclically.
There are two methods to implement this function: inheritance and category. The difference between category and inheritance is that the use of category is not to create a new class, but to add some methods based on the original class (the original class name is used, we do not need to modify the class name already written in other source files. We only need to import the h header file, and then modify the copy method to our new method.
1. Create an Objective-C category file. Set "MutableDeepCopy" to "Category" and "Category on" to "NSDictionary". Therefore, the generated file is NSDictionary + MutableDeepCopy. h and NSDictionary + MutableDeepCopy. m. The generated file name is easy to understand.
Ii. Source Code of two files:
NSDictionary + MutableDeepCopy. h
C ++ code
- # Import <Foundation/Foundation. h>
- @ Interface NSDictionary (MutableDeepCopy)
- -(NSMutableDictionary *) mutableDeepCopy;
- // Added the 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 an NSMutableDictionary object with the size of the original NSDictionary object
- NSArray * keys = [self allKeys];
- For (id key in keys)
- {// Read and copy each element cyclically
- 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 the 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];
- // Light copy
- NSMutableDictionary * dict3 = [dict1 mutableDeepCopy];
- // Deep copy
- [Arr1 addObject: @ "dd"];
- NSLog (@ "% @", dict2 );
- NSLog (@ "% @", dict3 );
- }
- Return 0;
- }
How to put a basic data type (int [], char []) in Objective C into a collection like NSDictionary,
First, a collection like NSDictionary can only store objective-c objects, so you need to encapsulate the basic data type first.
Apple encapsulates basic data types such as int into a class called NSNumber, first encapsulates the basic data you need into NSNumber and then you can add it to NSDictionary.
NSString is a string class. CString can be encapsulated into an NSString object and placed into a dictionary. Of course, adding NSNumber is also acceptable, but NSString is much more convenient.
The Code is as follows:
NSNumber * num = [NSNumber numberWithInt: 1];
NSString * string = [NSString stringWithCString: "Sample" encoding: NSUTF8StringEncoding];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: num, @ "num", string, @ "string", nil];
Why is the output of objective-c NSDictionary null?
You are so careless. ObjectsAndKeys: the front side is the value, the back is the key, and you cannot find it: @ "john ".