////////////////////////////////////////////////////////////////////
=========== Dictionary Collection ========== Dictionary Collection ========= Dictionary collection ============//
////////////////////////////////////////////////////////////////////
/*
Dictionaries are used to hold collections with mapping relationships (Key-value pairs) of data
A key-value is considered an entry, and the dictionary is a container for storing key-value pairs.
Unlike arrays, arrays access data by subscript, and array subscripts are unique
Dictionary accesses elements by key, key cannot be duplicated, value must be object
Key-value pairs are stored in the dictionary as unordered
Dictionaries are divided into: immutable dictionaries (nsdictionary) can be read only cannot be changed and mutable dictionary (nsmutabledictionary)
*/
Immutable dictionary, initialization dictionary, general key value is a string object, null value is represented by the Nsnull object
Nsdictionary *dic1 = [nsdictionary dictionarywithobject:@ "value1" forkey:@ "Key1"];
Nsdictionary *dic2 = [nsdictionary dictionarywithobjectsandkeys:@ "value2", @ "Key2", @ "Value3", @ "Key3", nil];
Nsdictionary *dic3 = [Nsdictionary dictionarywithdictionary:dic2];
Nsdictionary *dic4 = @{@ "Key1": @ "value1" @ "Key2": @ "value2"};//syntax sugar, between key-value pairs with "," separated by ":" Between the key and value values
Get the number of dictionaries
Nsinteger int1 = [Dic1 count];
NSLog (@ "%lu", int1);
Get value values by key value
NSString *result = [Dic2 objectforkey:@ "Key2"];//takes the value out of the key value to remove the Key2 corresponding value
NSLog (@ "%@", result);
The key value is taken out according to value, and all key values can be removed
Nsarray *RESULT2 = [Dic2 allkeysforobject:@ "value2"];//remove the key value based on value (all key values can be taken)
NSLog (@ "%@", RESULT2);
Takes all key values to return an array, which is received with the array Nsaarry
Nsarray *keyarray = [dic3 AllKeys];
NSLog (@ "%@", Keyarray);
Remove all value values
Nsarray *valuearray = [dic3 allvalues];
NSLog (@ "%@", ValueArray);
Converts the key value of a dictionary into an enumeration object for traversing
Nsenumerator *enumerator = [Dic2 keyenumerator];
//=========================================//
Variable dictionary/////////
Initialize, similar to immutable dictionaries
Nsmutabledictionary *DIC4 = [Nsmutabledictionary dictionary];
Nsmutabledictionary *dic5 = [nsmutabledictionary dictionarywithobjectsandkeys:@ "value1", @ "Key1", @ "value2", @ "Key2", NIL];
Add a dictionary to another
[Dic4 Addentriesfromdictionary:dic1];
Add elements to a dictionary
[Dic5 setobject:@ "Value3" forkey:@ "Key3"];
Set the content of DIC5 to the same content as Dic1
[Dic5 Setdictionary:dic1];
Removes all elements. Depending on the key value, you can also set the key value to array to pass in the array.
[Dic5 removeallobjects];
[Dic5 removeobjectforkey:@ "Key1"];
[Dic5 Removeobjectsforkeys:valuearray];
Fast Traversal method
For (ID key in dic5) {
ID obj = [dic5 Objectforkey:key];
NSLog (@ "%@", obj);
}
Common traversal methods
Nsarray *akey = [Dic5 AllKeys];
Nsinteger length = [Akey count];
for (int i = 0; i<length; i++) {
ID keynew = [akey objectatindex:i];
ID obj = [dic5 objectforkey:keynew];
NSLog (@ "%@", obj);
}
Using enumerator traversal
Nsenumerator *EMU1 = [Dic5 keyenumerator];
ID key = [emu1 nextobject];
while (key) {
ID obj = [dic5 Objectforkey:key];
NSLog (@ "%@", obj);
key = [EMU1 nextobject];
}
==oc== Dictionary Collection = =