The use of OC base variable dictionaries and immutable dictionaries

Source: Internet
Author: User
Tags allkeys

The use of OC base mutable dictionaries with immutable dictionaries 1. Immutable dictionaries

1.1 Creating an immutable dictionary

        //Create a dictionary//Note://1, the number of elements is even//2, each of the two elements is a key-value pair//3, value in front, key in rearNsdictionary *dic = [[Nsdictionary alloc] Initwithobjectsandkeys:@"Huang",@"name",@" -",@" Age", nil]; NSLog (@"%@", DIC); //use a dictionary to create another dictionaryNsdictionary *dic2 =[[Nsdictionary alloc] initwithdictionary:dic]; //Note://the number of 1,value and keys must be the same//The order of the elements within the 2,values and keys correspondsNsdictionary *dic3 = [[Nsdictionary alloc] Initwithobjects:[nsarray arraywithobjects:@"Huang",@" -", nil] Forkeys:[nsarray arraywithobjects:@"name",@" Age", Nil]]; NSLog (@"%@", DIC3); //new ways to create arraysNsarray *arr = @[@"1",@"2",@"3"]; Nsdictionary*DIC4 = [Nsdictionary Dictionarywithobjectsandkeys:@" -",@" Age",@"Huang",@"name", nil]; Nsdictionary*DIC5 =[Nsdictionary DICTIONARYWITHDICTIONARY:DIC4]; Nsdictionary*DIC6 = [Nsdictionary dictionarywithobjects:@[@"Huang",@" -"] Forkeys:@[@"name",@" Age"]]; //new ways to create dictionariesNsdictionary *dic7 = @{@"name":@"Huang",@" Age":@" -"};

1.2 Common operations for immutable dictionaries

        //common operations for dictionariesNsinteger count =[Dic7 Count]; NSLog (@"%ld", Count); //to get a value by keyNSString *str = [Dic7 valueforkey:@"name"]; NSLog (@"%@", str); //get all the keysNsarray *allkeys =[Dic7 AllKeys]; NSLog (@"%@", AllKeys); //get all the valueNsarray *allvalues =[Dic7 allvalues]; //gets a value corresponding to the keyNsdictionary *dic8 = @{@"name":@"Huang",@" Age":@" the",@"score":@" the"}; Nsarray*ARR1 = [Dic8 allkeysforobject:@" the"]; NSLog (@"%@", arr1); Nsdictionary*DIC9 = @{@"Dic8":d Ic8,@"Dic7":d Ic7}; NSLog (@"%@", DIC9); //nesting of arrays and dictionariesNsarray*array1 = @[@"1",@"2",@"3"]; Nsdictionary*dictionary1 = @{@"name":@"xiaoming",@" Age":@"Ten"}; Nsdictionary*dictionary2 = @{@"Array": Array1,@"Dictionary":d Ictionary1}; NSLog (@"%@", Dictionary2); Nsarray*array2 =@[array1,dictionary1]; NSLog (@"%@", array2); //the traversal of a dictionaryNsdictionary *dictionary3 = @{@"name":@"Huang",@" Age":@" -",@" Number":@" -"}; Nsarray*ALLKEYS1 =[Dictionary3 AllKeys];  for(NSString *keyinchallKeys1) {NSString*str =[Dictionary3 Objectforkey:key]; NSLog (@"%@=%@", KEY,STR); }

2. Variable Dictionaries

2.1 Creation and use of variable dictionaries

Nsdictionary *dic = @{@"name":@"xiaoming",@" Age":@"Ten"}; Nsdictionary*dic1 = @{@" Age":@" the",@"score":@" -"}; Nsmutabledictionary*mudic =[[Nsmutabledictionary alloc] init]; //add elements from a dictionary to a mutable dictionary//Note://when DIC has the same key as a mutable dictionary, the key is not added, but the value corresponding to the key is overwritten. [Mudic Addentriesfromdictionary:dic]; NSLog (@"%@", Mudic);        [Mudic Addentriesfromdictionary:dic1]; NSLog (@"%@", Mudic); //Add an element//Note://1,setobject:forkey: Adds an element when key is not present and modifies the element value when key is present[Mudic SetObject:@"mingming"Forkey:@"Nickname"]; [Mudic setobject:@"Xiaohua"Forkey:@"name"]; NSLog (@"%@", Mudic); //Delete an element[Mudic Removeobjectforkey:@"name"]; NSLog (@"%@", Mudic); //Delete all elements of the keys array[Mudic removeobjectsforkeys:@[@"Nickname",@"AAA"]]; NSLog (@"%@", Mudic); //Delete all elements within a dictionary[Mudic removeallobjects]; NSLog (@"%@", Mudic); //Modify Dictionary[Mudic Setdictionary:dic]; NSLog (@"%@", mudic);

The use of OC base variable dictionaries and immutable dictionaries

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.