Dark Horse Programmer _ Learning iOS Dictionary common methods

Source: Internet
Author: User
Tags delete key

The dictionary is unordered and the array is ordered. Dictionaries are divided into: mutable dictionaries and immutable dictionaries

Immutable Dictionary Objects

Nsdictionary * Dict = [[Nsdictionary alloc]initwithobjectsandkeys:@ "one", @ "1", @ "one", @ "2", @ "three", @ "3", @ "four", @ "4 ", nil];
Value = key
Initialization must be one by one the contents of the corresponding dictionary are key-value pairs Key->value content can be arbitrary object pointers
Key is generally nsstring * value can be any object
Cannot appear duplicate key key is unique

Nsdictionary * Dict1 = [nsdictionary dictionarywithdictionary:dict];

Nsarray * values = [Nsarray arraywithobjects:@ "one", @ "one", @ "three", nil];
Nsarray * keys = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", nil];//key-value to correspond
Nsdictionary * Dict2 = [[Nsdictionary alloc]initwithobjects:values Forkeys:keys];

[Dict count]//Key-value pairs must appear in pairs
[Dict objectforkey:@ "5"]//to find the corresponding value according to key

Create an enumerator for key to pass the key element address in the dictionary to the enumerator
Nsenumerator * Enumer1 = [dict keyenumerator];
ID obj;
while (obj = [Enumer1 nextobject]) {
NSLog (@ "%@", [Dict objectforkey:obj]);

Create a value enumerator (not used in general, meaningless)
Nsenumerator * Enumer2 = [dict objectenumerator];
while (obj = [Enumer2 nextobject]) {
NSLog (@ "%@", obj);
Fast enumeration of enumerations is key (cannot change data, read only)
For (ID key in dict) {
NSLog (@ "%@", key);

Nsarray * array = [urlitemdict allvalues];//Get all value
Nsarray * array = [urlitemdict allkeys];//gets all key------variable Dictionary objects

Variable Dictionary objects

Nsmutabledictionary * Dict = [[Nsmutabledictionary alloc]initwithobjectsandkeys:@ "one", @ "1", @ "one", @ "2", @ "three", @ " 3 ", @" four ", @" 4 ", nil];

[Dict setobject:@ "Seven" forkey:@ "7"];//increase key value to key already exists then modify value does not exist add
[Dict setobject:@ "Nine" forkey:@ "1"];//Modify value
[Dict removeobjectforkey:@ "1"];//Delete key value pair

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.