Implementation of the dictionary in oC

Source: Internet
Author: User

I. Basic concepts of dictionaries

The dictionary (nsdictionary, nsmutabledictionary) in foundation is a data set composed of key-value pairs, just as we define words in the dictionary.

Key (key) is used to find the corresponding value. Key is usually a String object or any other type of object. in a dictionary object, the key value must be unique.

In addition, the key and value of the dictionary object cannot be null (nil). If you need to add a null value to the dictionary, you can add the nsnull object.

Ii. Immutable dictionary-nsdictionary

1: Initialize (with one element and multiple elements ):

// Initialize with a key-Value Object

Nsdictionary * DIC = [nsdictionary dictionarywithobject: @ "Lu can sample" forkey: @ "Lucan"]; nslog (@ "% @", DIC ); nslog (@ "% @", [DIC objectforkey: @ "Lucan"]); // output the number of DIC key-value pairs nslog (@ "% d", DIC. count); // create a key-Value Pair nsdictionary * dic1 = [nsdictionary dictionarywithobject: @ "Lu can lab 2" forkey: @ "sample"] using multiple methods; nslog (@ "% @", [dic1 objectforkey: @ "sample"]); nsdictionary * [email protected] {@ "first": @ "2301 ", @ "Sec": @ "2034"}; nsdictionary * dic3 = [nsdictionary dictionarywithobjectsandkeys: @ "Liu Xiang", @ "name", @ "sample", @ "name1 ", nil]; // output result nslog (@ "---- % @", dic2, dic3 ); // put vaule and key in an array into a variable array nsarray * [email protected] [@ 123, @ 668, @ 345]; nsarray * [email protected] [@ "first ", @ "swcond", @ "third"]; nsdictionary * dic4 = [nsdictionary dictionarywithobject: Values forkey: Key]; nslog (@ "xxxxxxxxxxxx % @", dic4 ); // sort nsarray * arry = [dic4 keyssortedbyvalueusingselector: @ selector (compare :)]; nslog (@ "++ % @", arry ); // use an existing dictionary object to initialize another new dictionary object (create a mutable object) nsdictionary * arry1 = [[nsdictionary alloc] initwithdictionary: dic4]; nslog (@ "ooooooo % @", arry1); // Save the object to the content file nsstring * Path = @ "/users/Apple/desktop/test. plist "; [dic2 writetofile: path atomically: Yes]; // read the dictionary object nsdictionary * Data = [nsdictionary dictionarywithcontentsoffile: path] from the previously saved file; nslog (@ "XXXXX % @", data); // dictionary traversal key1 is equivalent to I in a [I], and dic4 is the Dictionary defined by itself (ID key1 in dic4) {ID vaule = [dic4 objectforkey: key1]; nslog (@ "qqqqqq % @", key, vaule);} //-(void) removeobjectforkey :( ID) akey; // delete key-value pairs nsmutabledictionary * dic7 = [nsmutabledictionary dictionarywithdictionary: dic2]; [dic7 removeobjectforkey: @ "Sec"]; nslog (@ "% @", dic7 ); // Add [dic7 setobject: @ "3412" forkey: @ "Sec"]; nslog (@ "% @", dic7) If no key value is replaced ); // Add a dic4 dictionary without sequence [dic7 addentriesfromdictionary: dic4]; nslog (@ "% @", dic7 );

 

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.