Objective-C Learning Record 6 -- dictionary

Source: Internet
Author: User

1. nsdictionary and nsmutabledictionary

 

Nsdictionary dictionarywithobjectsandkeys :~, Nil

Create a dictionary using a key-Value Pair and end with the NIL flag

Nsdictionary initwithobjectsandkeys:

Use Key-value pairs to initialize the dictionary, and use nil to indicate the end.

Dictionary count calculates the length of its dictionary.

Dictionary keyeunmerator stores all the keys in nseunmerator, And you can quickly enumerate the values of the keys.

Dictionary objectforkey: The key is used to query the value.

Demo:

# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code nsdictionary * dictionary = [nsdictionary dictionarywithobjectsandkeys: @ "I Am a value", @ "I am key1", @ "I Am a value 2 ", @ "I am key2", nil]; // obtain the number of dictionaries int COUNT = [dictionary count]; nslog (@ "Number of dictionaries: % d", count ); // obtain all the key values in the dictionary. nsenumerator * enumeratorkey = [dictionary keyenumerat Or]; // quickly enumerate all key values for (nsobject * object in enumeratorkey) {nslog (@ "traversal key value: % @", object );} // obtain all value values in the dictionary nsenumerator * enumeratorvalue = [dictionary objectenumerator]; // quickly enumerate all values for (nsobject * object in enumeratorvalue) {nslog (@ "traversal value: % @", object);} // locate value nsobject * object = [dictionary objectforkey: @ "I am key2"] through key; if (object! = Nil) {nslog (@ "the value found through key is: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil ); [pool release]; return retval ;}

 

2. nsmutabledictionary is a subclass of nsdictionary, so it also inherits its methods.

 

[Nsmutabledictionary dictionarywithcapacity: 10];
// Create a dictionary with a length of 10. However, if the dictionary content exceeds 10, it will automatically increase.
[Nsmutabledictionary initwithcapacity: 10]; // The initialization length is 10; [dictionary setobject :~ Forkey; ~]; // X add data to a variable dictionary; [dictionary removeallobjects]; // delete all data; removeobjectforkey: // Delete the corresponding value of the key;
# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code // create a dictionary object, the initialization length is 10 nsmutabledictionary * dictionary = [nsmutabledictionary dictionarywithcapacity: 10]; // dynamically add data to the dictionary [dictionary setobject: @ "added value1" forkey: @ "key1"]; [dictionary setobject: @ "added value2" forkey: @ "key2"]; // locate value nsobject * objec through key T = [dictionary objectforkey: @ "key2"]; If (object! = Nil) {nslog (@ "the value found through key is: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil ); [pool release]; return retval ;}

 

 
[Nsmutabledictionary dictionarywithcapacity: 10];
// Create a dictionary with a length of 10. However, if the dictionary content exceeds 10, it will automatically increase.
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.