OC Learning---The Nsdirctionary class and the Nsmutabledirctionary class in the foundation framework

Source: Internet
Author: User
Tags allkeys map class

Yesterday I learned the Nsarray class and the Nsmutablearray class in the foundation framework: http://blog.csdn.net/jiangwei0910410003/article/details/ 41809719, today look at the foundation framework of the Nsdirctionary class, Nsmutabledirctionary class, this and Java map class very much want to, OC called dictionary, Java called Map, and dictionaries are unordered, This is not the same as Nsarray, the map in Java is also unordered, through the hash value to retrieve the elements.


First, Nsdirctionary class

main.m//19_nsdictionary////Created by Jiangwei on 14-10-12.//Copyright (c) 2014 Jiangwei. All rights reserved.//#import <foundation/foundation.h>//data set composed of key-value pairs Key-value int main (int argc, const char * argv []) {@autoreleasepool {//1.---------------------dictionary creation Nsarray *array1 = [Nsarray arraywithobjects:@ "Zha        Ngsan ", @" Zhangfei ", nil];                Nsarray *array2 = [Nsarray arraywithobjects:@ "Lisi", @ "Liping", nil];  First element: key:@ "Zhang" value:array1//second element: key:@ "li" value:array2 nsdictionary *dict = [[Nsdictionary alloc]        initwithobjectsandkeys:array1,@ "Zhang", array2,@ "Li", nil];                NSLog (@ "%@", dict);                        Storage of an element nsdictionary *dict2 = [nsdictionary dictionarywithobject:array1 forkey:@ "Zhang"];                        2.---------------------get all key Nsarray *allkeys = [Dict AllKeys]; 3.---------------------get all the value//may be a two-dimensional array nsarray *allvalues = [Dict allvalues];                        4.---------------------through key to obtain value nsarray *values = [dict objectforkey:@ "Zhang"];        5.---------------------optimized syntax nsdictionary *dict3 = @{@ "Zhangsan": array1,@ "Lisi": array2};                NSLog (@ "%@", dict3);                    Nsarray *array4 = dict3[@ "Zhang"]; } return 0;}


1. How to create

1. Creation of the---------------------dictionary nsarray *array1 = [Nsarray arraywithobjects:@ "Zhangsan", @ "Zhangfei", nil]; Nsarray *array2 = [Nsarray arraywithobjects:@ "Lisi", @ "Liping", nil];//first element: key:@ "Zhang" value:array1//second element: key:@ " Li "  value:array2nsdictionary *dict = [[Nsdictionary alloc] initwithobjectsandkeys:array1,@" Zhang ", array2,@" Li ", NIL]; NSLog (@ "%@", dict);
We can see that the creation of the Nsdirctionary class is based on Nsarray, and then the other and the Java map is almost, generally key-value form, of course, it is also necessary to note that the end is: nil

Operation Result:



The print result is a key-value style


2. Adding elements

Storage of an element nsdictionary *dict2 = [nsdictionary dictionarywithobject:array1 forkey:@ "Zhang"];

3. Get key for all elements

2.---------------------get all Keynsarray *allkeys = [Dict AllKeys];

4. Get the value of all elements

3.---------------------get all value//may be a two-dimensional array nsarray *allvalues = [Dict allvalues];

5. Get Value by key

4.---------------------get Valuensarray *values by key = [Dict objectforkey:@ "Zhang"];

6, Nsdirctionary fast creation way and access way

5.---------------------optimized syntax nsdictionary *dict3 = @{@ "Zhangsan": array1,@ "Lisi": array2}; NSLog (@ "%@", dict3); Nsarray *array4 = dict3[@ "Zhang"];


Second, Nsmutabledirctionary class

main.m//20_nsmutabledictionary////Created by Jiangwei on 14-10-12.//Copyright (c) 2014 Jiangwei. All rights reserved.//#import <foundation/foundation.h>//variable dictionary int main (int argc, const char * argv[]) {@autorelea Sepool {//1.-------------Create a mutable dictionary with a size of 3 nsmutabledictionary *md1 = [[Nsmutabledictionary alloc] Initwithcapa        City:3];                        Nsarray *array1 = [[Nsarray alloc] initwithobjects:@ "Zhangsan", @ "lis", nil];                2.-------------add element [md1 setobject:array1 forkey:@ "Zhang"];        Nsmutabledictionary *MD2 = [[Nsmutabledictionary alloc] initwithcapacity:3];                        [MD2 ADDENTRIESFROMDICTIONARY:MD1];  3.-------------delete element [md1 removeobjectforkey:@ "Zhang"];//with key delete [md1 removeallobjects];//Delete all elements [md1  removeobjectsforkeys:array1];//Delete//4 with value.-------------Loop through the dictionary//Fast traverse for (nsstring *key in md1) {Nsarray *values = [Md1 objectforKey:key];            For (NSString *v in values) {NSLog (@ "%@", V);        }}//normal traversal nsarray *allkey = [md1 AllKeys];        for (int i=0;i<allkey.count;i++) {Nsarray *value = [md1 objectforkey:allkey[i]]; }//Dictionary is unordered} return 0;}
The previous Nsdirctionary class is an immutable dictionary, and there is a corresponding mutable dictionary in the same OC: Nsmutabledirctionary

1. Create a mutable dictionary

1.-------------Create a mutable dictionary with a size of 3NSMutableDictionary *md1 = [[Nsmutabledictionary alloc] initwithcapacity:3]; Nsarray *array1 = [[Nsarray alloc] initwithobjects:@ "Zhangsan", @ "lis", nil];
Create a mutable dictionary that can specify its size, of course, when the capacity is full, he automatically expands


2. Adding elements

2.-------------add element [md1 setobject:array1 forkey:@ "Zhang"]; Nsmutabledictionary *MD2 = [[Nsmutabledictionary alloc] initwithcapacity:3]; [MD2 ADDENTRIESFROMDICTIONARY:MD1];


3. Deleting elements

3.-------------delete element [md1 removeobjectforkey:@ "Zhang"];//with key delete [md1 removeallobjects];//Delete all elements [md1 removeobjectsforkeys:array1];//Delete with value

4. Traverse the dictionary

4.-------------loop traversal dictionary//Fast traverse for (NSString *key in md1) {    Nsarray *values = [md1 Objectforkey:key];    For (NSString *v in values) {        NSLog (@ "%@", v);    }} Normal traversal Nsarray *allkey = [md1 allkeys];for (int i=0;i<allkey.count;i++) {    Nsarray *value = [Md1 objectforkey:allkey [i]];}


Summary

This article introduces the Nsdirctionary class and the Nsmutabledirctionary class in OC, where their use is critical and often used.





OC Learning---The Nsdirctionary class and the Nsmutabledirctionary class in the foundation framework

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.