The Dictionary of Objective-c

Source: Internet
Author: User
Tags allkeys


1 // Dictionary: (keyword value)
 2 // NSArray * array = [NSArray array]; // empty array
 3 // NSDictionary * dictionary = [NSDictionary dictionary]; // empty dictionary
 4 NSDictionary * my = [NSDictionary dictionaryWithObject: @ "objective" forKey: @ "key"];
 5 NSLog (@ "% @", my);
 6 NSDictionary * to = [NSDictionary dictionaryWithObjectsAndKeys: @ "123", @ "abc", @ "456", @ "efg", nil]; // Create a dictionary containing multiple values
 7 NSLog (@ "% @", to);
 8 NSDictionary * me = @ {
 9 @ "a": @ "1",
10 @ "b": @ "2"
11};
12 NSLog (@ "% @,% li", me, me.count);
13 NSString * s = [me objectForKey: @ "a"]; // The object corresponding to the key value
14 NSString * ss = me [@ "b"]; // Almost like an array
15 NSLog (@ "% @,% @", ss, s);
16
17 // NSArray * keyArr = [me allKeys];
18 // for (NSString * key in keyArr)
19 // {
20 // NSLog (@ "% @ =% @", key, me [key]);
twenty one //        }
twenty two       
23 NSDictionary * niubi = [NSDictionary dictionaryWithObjectsAndKeys: @ "liyuanfang", @ "direnjie", @ "Moran", @ "fuermosi", @ "kenanfushou", @ "kenan", nil]; // Create a multiple dictionary
24 NSArray * keyArr = [niubi allKeys]; // Get all key values in the dictionary
25 for (NSString * key in keyArr)
26 {
27 NSLog (@ "% @ 问% @ What do you think about this?", Key, niubi [key]);
28}
29 / ************************************************ ********************************** /
30 // Retrieve all values from the dictionary
31 NSArray * valueArr = [me allValues];
32 NSLog (@ "take the value corresponding to all keys in the dictionary% @", valueArr);
33
34 // Since the dictionary exists, the developer must think of using it conveniently
35 // However, there are methods that can fetch keys or values independently: allKeys and allValues
36 // Because they are array attributes, they need to be placed in the newly created array object
37
38 // An effective method for traversal in OC language in enumerator,
39 // When applying, generally get the enumerator through ..... Enumerator and store it in its corresponding type NSEnumerator object
40 // This object will have a method called nextObject
41 // First get the enumerator of the key in the dictionary, then traverse the enumerator and get the value corresponding to the key
42 NSEnumerator * e = [me keyEnumerator];
43 id obj;
44 while (obj = [e nextObject]) {
45 NSLog (@ "% @ =% @", obj, me [obj]);
46}
47
48 // Get the enumerator corresponding to value
49 NSEnumerator * a = [me objectEnumerator];
50 while (obj = [a nextObject])
51 {
52 NSLog (@ "% @", obj);
53}
54
55 // Key and object enumeration block, stop stands for traversal stop
56
57 [me enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL * stop) {
58
59 NSLog (@ "key =% @, value =% @", key, obj);
60}]; 





The Dictionary of Objective-c


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.