<span style = "font-size: 18px;"> # import <Foundation / Foundation.h>
int main (int argc, const char * argv []) {
@autoreleasepool {
//dictionary
/ *
Dictionary NSDictionnary // NSMutableDictionary
1. Concept: A dictionary is a set of keywords: 'key' and its defined value 'value', and other sets of key-value pairs consisting of key-value.
Generally speaking, 'key' is of general type 'NSString', and 'value' is any object in OC. For a dictionary, we access the value by key.
2. Why do you need a dictionary after you have an array? When querying information from a large amount of data or frequently, using a dictionary is more efficient because the dictionary is queried using keys
Of optimized storage methods.
3. In OC we use 'NSDictionary' and 'NSMutableDictionary' to represent the dictionary. Of which "NSMutableDictionary"
Is a subclass of "NSDictionary"
* /
//initialization
// Create an empty dictionary
NSDictionary * dictionary1 = [[NSDictionary alloc] init];
NSDictionary * dictionary2 = [NSDictionary dictionary]; // Own initialization method
// Create a dictionary of key-value pairs
// Create a dictionary with a key-value pair
NSDictionary * dictionary3 = [NSDictionary dictionaryWithObject: @ "Rick" forKey: @ "name"];
// NSDictionary * dictionary3 = [NSDictionary dictionaryWithObjects: @ "Rick" forKeys: @ "name"]; this is an error more than s
// Create a dictionary of multiple key-value pairs
// value is in front of name
NSDictionary * dictionary4 = [NSDictionary dictionaryWithObjectsAndKeys: @ "Rick", @ "name", @ 25, @ "age", @ "GZ", @ "address", nil]; // Enter multiple key value pairs, @ 25 Delete 25 without error but don't write like this
// The elements stored in the dictionary must be object types. If it is a basic data type, you must first encapsulate these basic data types, and then
// put in these arrays
// value goes again, after the key, value and key must appear in pairs such as dictionary4
NSLog (@ "dictionary4 =% @", dictionary4); // The output is out of order
// Note that the keys in the dictionary are out of order
// Create a dictionary based on a dictionary
NSDictionary * dictionary5 = [NSDictionary dictionaryWithDictionary: dictionary4]; // Dictionary
NSLog (@ "dictionary5 =% @", dictionary5);
if (dictionary5 == dictionary4)
{
NSLog (@ "aaa"); // No output
}
NSString * string = @ "aaaa";
NSString * string2 = [NSString stringWithString: string];
if (string == string2)
{
NSLog (@ "bbb"); // Shallow copy execution
}
// Create key-value pairs with @ {} arrays with @ []
// create dictionary with new syntax
NSDictionary * dictionary6 = @ {};
// New syntax creates a key-value pair
NSDictionary * dictionary7 = @ {@ "name": @ "Rice"};
// Create multiple key-value pairs
NSDictionary * dictionary8 = @ {@ "name": @ "Rick", @ "age": @ 25, @ "address": @ "GZ"};
NSLog (@ "dictionary8 =% @", dictionary8);
// Read dictionary from local
NSDictionary * dictionary9 = [NSDictionary dictionaryWithContentsOfFile: @ ""];
// Network reads dictionary
NSDictionary * dictionary10 = [NSDictionary dictionaryWithContentsOfURL: [NSURL URLWithString: @ "http://www.baidu.com"]];
// Number of key-value pairs in the dictionary
NSUInteger count = dictionary4.count;
NSLog (@ "% lu", count);
// how to access our dictionary
// Access the corresponding value in the dictionary by key
NSString * name = [dictionary4 objectForKey: @ "name"];
NSLog (@ "name =% @", name);
//Equivalent to
// new syntax
NSString * newName = dictionary4 [@ "name"];
NSLog (@ "newName =% @", newName);
// How to iterate through all the values in the dictionary
// means all keys in the dictionary
NSArray * keyArray = dictionary4.allKeys;
// The essence of dot syntax is set and get
// All values in the dictionary
NSArray * valuesArray = dictionary4.allValues;
NSLog (@ "valuesArray =% @", valuesArray);
// use for loop output
for (int i = 0; i <valuesArray.count; i ++)
{
NSLog (@ "% @ =% @", keyArray [i], valuesArray [i]);
NSLog (@ "% @ =% @", keyArray [i], [dictionary4 objectForKey: keyArray [i]]);
NSLog (@ "% @ =% @", keyArray [i], dictionary4 [keyArray [i]]); // is using the new syntax
}
// use fast enumeration
for (NSString * key in keyArray)
{
NSLog (@ "% @ =% @", key, [dictionary4 objectForKey: key]);
// Assign the value in keyArray to key
//Equivalent to
NSLog (@ "% @ =% @", key, dictionary4 [key]);
}
// Use an enumerator to traverse
NSEnumerator * enumerator = [keyArray objectEnumerator];
NSString * key;
while (key = [enumerator nextObject])
{
NSLog (@ "% @ =% @", key, dictionary4 [key]);
}
// Cannot store the same key in an immutable dictionary, if the same key, only the value corresponding to the first key
NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "Rick", @ "name", @ 25, @ "age", @ "GZ", @ "address", nil];
NSLog (@ "--------------------");
for (NSString * key in [dictionary allKeys])
{
NSLog (@ "% @ =% @", key, dictionary [key]);
}
}
return 0;
}
</ span>