IOS Nsdictionary Dictionary

Source: Internet
Author: User

  1. Create a dictionary
  2. Nsdictionary *dic1 = [nsdictionary dictionarywithobject:@ "value" forkey:@ "key"];
  3. NSLog (@ "Dic1:%@", Dic1);
  4. For immutable nsdictionary *dic1 =@{@ "value": @ "key", @ "value1": @ "Key1"};
  5. Create multiple dictionaries
  6. Nsdictionary *dic2 = [Nsdictionary Dictionarywithobjectsandkeys:
  7. @ "value1", @ "Key1",
  8. @ "value2", @ "Key2",
  9. @ "Value3", @ "Key3",
  10. @ "Value4", @ "Key4",
  11. NIL];
  12. NSLog (@ "Dic2:%@", Dic2);
  13. To create a dictionary from an existing dictionary
  14. Nsdictionary *dic3 = [Nsdictionary dictionarywithdictionary:dic2];
  15. NSLog (@ "DIC3:%@", DIC3);
  16. Get value based on key
  17. NSLog (@ "Key3 value:%@", [dic3 objectforkey:@ "Key3"]);
  18. Get Dictionary number
  19. NSLog (@ "dic count:%d", dic3.count);
  20. All the key sets
  21. Nsarray *keys = [dic3 AllKeys];
  22. NSLog (@ "Keys:%@", keys);
  23. All Values Collection
  24. Nsarray *values = [dic3 allvalues];
  25. NSLog (@ "values:%@", values);
  26. Nsmutabledictionary *mutabledic = [[Nsmutabledictionary alloc] Initwithobjectsandkeys:
  27. @ "mvalue1", @ "Mkey1",
  28. @ "Mvalue2", @ "Mkey2", nil];
  29. To add an existing dictionary data
  30. [Mutabledic ADDENTRIESFROMDICTIONARY:DIC3];
  31. NSLog (@ "Mutabledic:%@", mutabledic);
  32. Adding a new key-value object
  33. [Mutabledic setvalue:@ "Set1" forkey:@ "SetKey1"];
  34. NSLog (@ "Set value for key:%@", mutabledic);
  35. Overwrite old dictionary data with new dictionary data
  36. [Mutabledic Setdictionary:dic2];
  37. NSLog (@ "Set dictionary:%@", mutabledic);
  38. Delete value based on key
  39. [Mutabledic removeobjectforkey:@ "Key1"];
  40. NSLog (@ "Removeforkey:%@", mutabledic);
  41. Fast traversal
  42. For (ID key in mutabledic) {
  43. NSLog (@ "key:%@ value:%@", Key, [Mutabledic Objectforkey:key]);
  44. }
  45. Enumeration traversal
  46. Nsenumerator *enumerator = [Mutabledic keyenumerator];
  47. ID key = [enumerator nextobject];
  48. while (key) {
  49. NSLog (@ "Enumerator:%@", [Mutabledic Objectforkey:key]);
  50. key = [Enumerator nextobject];
  51. }
  52. Delete an element based on the key array
  53. [Mutabledic Removeobjectsforkeys:keys];
  54. NSLog (@ "Removeobjectsforkeys:%@", mutabledic);
  55. [Mutabledic removeallobjects];
  56. Delete all elements
  57. NSLog (@ "Remove all:%@", mutabledic);
  58. }
  59. return 0;
  60. }

IOS Nsdictionary Dictionary

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.