iOS Development-objc-nsdictionary

Source: Internet
Author: User

In OC, the Foundation framework has dictionary classes, dictionary-immutable dictionaries (nsdictionary), and variable dictionaries (nsmutabledictionary), which are used as follows:

Immutable dictionaries:

1 //------------------immutable Dictionary------------------2     3 //Create a Dictionary object, and print the output4Nsdictionary *dic = [Nsdictionary dictionarywithobject:@"Onevalue"Forkey:@"Firstkey"];5NSLog (@"%@", DIC);6     7 //Create a Dictionary object and print the output8Nsdictionary *DICC = [Nsdictionary Dictionarywithobjectsandkeys:@"Onevalue",@"Firstkey",@"Twovalue",@"Secondkey",@"Threevalue",@"Thirdkey",@"Threevalue",@"Forthkey", nil];9NSLog (@"%@", DICC);Ten      One //outputs a value object corresponding to a key ANSLog (@"%@", [DICC Objectforkey:@"Firstkey"]); -      - //calculates the number of elements in a dictionary (a key-value pair is an element), and outputs a value corresponding to a key theNSLog (@"%lu,%@", [DICC Count], [DICC Objectforkey:@"Secondkey"]); -      -      - //use arrays to hold keys with the same value +Nsarray * Arraylastout = [DICC allkeysforobject:@"Threevalue"]; -NSLog (@"%@", arraylastout);

Variable dictionaries:

1 //------------------Variable Dictionary------------------2 //Create an empty mutable dictionary3Nsmutabledictionary *mdic = [[Nsmutabledictionary alloc]initwithcapacity:Ten];4     5 //Create a mutable dictionary and assign an initial value6Nsmutabledictionary * MDICC = [Nsmutabledictionary Dictionarywithobjectsandkeys:@"Onevalue",@"Firstkey",@"Twovalue",@"Secondkey",@"Threevalue",@"Thirdkey",@"Threevalue",@"Forthkey", nil];7     8 //assign the full value of a dictionary to another dictionary9 [MDic ADDENTRIESFROMDICTIONARY:MDICC];Ten      OneNSLog (@"%@", mDic); A      - //use set to update the value of a key in the dictionary, or update the value if key exists -[MDic SetValue:@"Changeonevalue"Forkey:@"Firstkey"]; theNSLog (@"%@", mDic); -      - //if the key value does not exist, a pair of Key-value key-value pairs is added -[MDic SetValue:@"NewValue"Forkey:@"Fifthkey"]; +NSLog (@"%@", mDic); -      + //completely replaced with the following dictionary contents A [MDICC setdictionary:dic]; atNSLog (@"%@", MDICC); -      -      - //Delete the contents of a dictionary-delete 1 -[MDICC Removeobjectforkey:@"Firstkey"]; -      in //Delete multiple -[MDICC removeobjectsforkeys:@[@"Firstkey",@"Secondkey"]]; to      + //Delete all content - [MDICC removeallobjects]; theNSLog (@"%@", MDICC);

Dictionary traversal:

1 //Dictionary Traversal2 //Dictionary Traversal Method 13      for(intx=0; x<mdicc.count; X + +) {4NSLog (@"%@", [MDICC OBJECTFORKEY:[[MDICC AllKeys] objectatindex:x]]);5     }6     7 //Dictionary Traversal Method 28      for(intx=0; x<mdicc.count; X + +) {9NSLog (@"%@", [[MDICC allvalues] objectatindex:x]);Ten     } One      A //Dictionary Traversal Method 3 -      for(IDKeyinchMDICC) { -NSLog (@"%@", [MDICC Objectforkey:key]); the     } -      - //Dictionary Traversal Method 4 -     IDdicx; +Nsenumerator *dicenu =[MDICC Keyenumerator]; -      while(dicx=[Dicenu Nextobject]) { +NSLog (@"%@", [MDICC objectforkey:dicx]); A     } at      - //Dictionary Traversal Method 5 -     IDDicenu; -Nsenumerator * Dicenum =[MDICC Objectenumerator]; -      while(dicenu=[Dicenum Nextobject]) { -NSLog (@"ddd%@", Dicenu); in}

iOS Development-objc-nsdictionary

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.