Objective-c Dictionary

Source: Internet
Author: User
Tags allkeys


1//Initialize an empty dictionary
2//nsdictionary *dictionary = [[Nsdictionary alloc] init];
3//Initialize an empty code
4//nsdictionary *dictionary1 = [Nsdictionary dictionary];
5//add a pair of key values while initializing
6//Nsdictionary *dictionary2 = [nsdictionary dictionarywithobject:@ "value" forkey:@ "key"];
7//adding many pairs of key values while initializing
8//the preceding value can be any type
9//followed by a key must be a string
TenNsdictionary *dictionary3 = [Nsdictionary Dictionarywithobjectsandkeys:@"Object",@"Key",@"Object1",@"Key1",@"Object2",@"Key2", nil];
One//a new Way of dictionary
A//preceded by a key is a value opposite to the key value of the previous method
-Nsdictionary *dictionary4 = @{@"Key":@"value",@"Key1":@"value 1"};
-NSLog (@"%@", Dictionary3);
theNSLog (@"%@", Dictionary4);
-
- //Objectforkey: Through the value of the key curvature dictionary
-NSLog (@"%@", [Dictionary4 Objectforkey:@"Key"]);
+//The second way of taking value
-NSLog (@"%@", dictionary4[@"Key1"]);
+
A
at//1 Use a dictionary to express your test scores Chinese 25 math 1000 English 3
-
-Nsdictionary *scrose = [Nsdictionary Dictionarywithobjectsandkeys:@" -",@"Chinese",@" +",@"Math",@"3",@"中文版", nil];
-[Scrose Objectforkey:@"Chinese"];
-[Scrose Objectforkey:@"Math"];
-[Scrose Objectforkey:@"Chinese version"];
inNSLog (@"%@ %@ %@", [Scrose Objectforkey:@"Chinese"],[scrose Objectforkey:@"Math"],[scrose Objectforkey:@"中文版"]);
-Nsdictionary *year = [Nsdictionary Dictionarywithobjectsandkeys:@"Germination",@"Spring",@"Flowering",@"Xia",@"Results",@"Autumn",@"Fade",@"Winter",@"Germination 1",@"Spring 1",@"Flowering 1",@"Summer 1",@"Results 1",@"Autumn 1",@"Fade 1",@"Winter 1", nil];
to//outputs a value corresponding to a key
+NSLog (@"%@", [Year objectforkey:year.allkeys[0]]);
-//Traverse all the key values
the//AllKeys is an array of all keys
*//count gets how many elements are in the number
$ for(NSString *keyinchYear.allkeys) {
Panax NotoginsengNSLog (@"%@ %@", Key,[year Objectforkey:key]);
-}
the
+
ANSLog (@"%@ %@ %@ %@", [Year Objectforkey:@"Spring"],[year Objectforkey:@"Xia"],[year Objectforkey:@"Autumn"],[year Objectforkey:@"Winter"]);
the





Execution Result:






Objective-c Dictionary


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.