[Oc learning diary] dictionary and oc learning diary dictionary

Source: Internet
Author: User

[Oc learning diary] dictionary and oc learning diary dictionary

1 // create a dictionary 2 NSDictionary * d1 = [NSDictionary dictionary]; 3 // set the content of the created dictionary to the content of the specified file 4 NSString * path = @ ""; // File path 5 NSDictionary * d2 = [NSDictionary dictionaryWithContentsOfFile: path]; 6 // create a dictionary with a key value 7 NSDictionary * d3 = [NSDictionary dictionaryWithObject: @ "s" forKey: @ "s"]; 8 // create a dictionary key-value pair with multiple key values must correspond to 9 // The value of the previous key in the last 10 NSDictionary * d4 = [NSDictionary dictionaryWithObjectsAndKeys: @ "ss ", @ "2", 11 @ "ss", @ "1", 12 nil]; 13 14 // create a dictionary using arrays 15 NSArray * key = @ [@ "2 ", @ "3", @ "4"]; 16 NSArray * obj = @ [@ "ss", @ "ff", @ "gg"]; 17 NSDictionary * d5 = [NSDictionary dictionaryWithObjects: obj forKeys: key]; 18 // convenient mode 19 // key previous value 20 NSDictionary * d6 =@{ 21 @ "2 ": @ "d", 22 @ "3": @ "d "}; 23 24 // init initialization 25 // set the initialized dictionary content to the specified file content 26 NSDictionary * dd = [[NSDictionary alloc] initWithContentsOfFile: path]; 27 // initialize a dictionary with multiple key-values (using arrays) 28 NSDictionary * dd1 = [[NSDictionary alloc] initWithObjects: obj forKeys: key]; 29 // initialize a dictionary with multiple key-values 30 NSDictionary * dd2 = [[NSDictionary alloc] initWithObjectsAndKeys: 31 @ "ss", @ "2 ", 32 @ "ss", @ "1", nil]; 33 34 35 36 // basic dictionary operation 37 // The dictionary is unordered. Duplicate keys are not allowed. Repeated values are allowed. 38 // All keys are returned. NSLog (@ "% @", [dd2 allKeys]); 40 // return all values 41 NSLog (@ "% @", [dd2 allValues]); 42 // return the key 43 NSLog (@ "% @", [dd2 allKeysForObject: @ "ss"]) corresponding to all values; 44 // return the most common and important NSLog values of the corresponding key (@ "% @", [dd2 objectForKey: @ "ss"]); 46 // key-value pairs 47 NSLog (@ "% zi", dd2.count); 48 49 50 // unchangeable dictionary 51 // create 52 NSMutableDictionary * dic = [NSMutableDictionary dictionary]; 53 // allocate space 54 NSMutableDictionary * dic1 = [[NSMutableDictionary alloc] failed: 10]; 55 // create and assign a value of 56 NSMutableDictionary * dic2 = [NSMutableDictionary attributes: @ "ss ", @ "2", 57 @ "ss", @ "1", nil]; 58 // Add a key-Value Pair 59 [dic2 setObject: @ "s" forKey: @ "sdads"]; 60 // delete all key-value pairs 61 [dic2 removeAllObjects]; 62 // Delete the key-Value Pair 63 [dic2 removeObjectForKey: @ "sd"]; 64 65 66 // dictionary traversal 67 // fast traversal 68 for (id key in dic2) {69 NSLog (@ "% = % @", key, [dic2 objectForKey: key]); 70} 71 // code block traversal 72 [dic2 enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL * stop) {73 NSLog (@ "% @", key, obj); 74}]; 75 // iterator traverses 76 // output key and value 77 NSEnumerator * en = [dic2 keyEnumerator]; // load all keys into the array 78 id key2 = nil; 79 while (key2 = [en nextObject]) {80 NSLog (@ "% @", key2, [dic2 objectForKey: key2]); 81} 82 NSEnumerator * en1 = [dic2 objectEnumerator]; // load all values into the array 83 // only 84 id obj3 = nil can be output; 85 while (obj3 = [en1 nextObject]) {86 NSLog (@ "% @", obj ); 87} 88 89 90 // if the key is null, an exception value is returned to the control, which is the end mark.

Add:

If You encapsulate a custom class object into a dictionary

You can use the setValuesForKeysWithDictionary method to assign values to object attributes. However, the keys encapsulated in the dictionary must be the same as the attribute names in the class and the number of attributes cannot exceed the number of attributes in the class.

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.