Cocoa Foundation notes-3

Source: Internet
Author: User
// Nsdictionary is a data set composed of key-value pairs. Key) the value must be unique ************* * *** // dictionary initialization nsdictionary * dic1 = [nsdictionary dictionarywithobject: @ "value" forkey: @ "key"]; // output: {key = value} nsdictionary * dic2 = [nsdictionary dictionarywithobjects: @ "V1", @ "K1 ", @ "V2", @ "K2", @ "V3", @ "K3", nil]; nsdictionary * dic3 = [nsdictionary dictionarywithdictionary: dic2]; // obtain the number of dictionaries int COUNT = [dic2 count]; // output: Count = 3 (with three key-value pairs) // obtain the nsstring * string = [dic2 objectforkey: @ "K2"] According to the key name; // output: string = v2 // obtain all keys of the dictionary and valuensarray * keyarray = [dic2 allkeys]; // output: keyarray = {K1, K2, k3} nsarray * valuearray = [dic2 allvalues]; // output: valuearray = {V1, V2, v3}/***************** variable dictionary ******************* ** // create a dictionary nsmutabledictionary * mutabledic = [[nsmutabledictionary alloc] initwithobjectsandkeys: @ "V1", @ "K1", @ "V2", @ "K2", @ "V3", @ "K3", nil]; // Add a key-Value Pair // method 1 nsdictionary * dic4 = [nsdictionary dictionarywithobject: @ "V4" forkey: @ "K4"]; [mutabledic addentriesfromdictionary: dic4]; // method 2 [mutabledic setvalue: @ "object" forkey: @ "K5"]; // create an empty variable dictionary nsmutabledictionary * mutabledic2 = [nsmutabledictionary dictionary]; [mutabledic2 setdictionary: mutabledic]; // optional * mutabledic2 = [nsmutabledictionary dictionarywithobject: @ "one" forkey: @ "K"]; // Delete the element based on the key name [mutabledic removeobjectforkey: @ "K3"]; // delete a key-Value Pair nsarray * keys = [nsarray arraywithobjects: @ "K1", @ "K2", @ "K3", nil]; [mutabledic removeobjectsforkeys: Keys]; // delete all elements [mutabledic removeallobjects]; ********************* * *** // generally traverse for (INT Index = 0; index <[mutabledic count]; index ++) {nsstring * object = [mutabledic objectforkey: [mutabledic allkeys] objectatindex: Index]; nslog (@ "% @", object) ;}// quick enumeration for (ID key in mutabledic) {nsstring * object = [mutabledic objectforkey: Key]; nslog (@ "% @", object );} // enumerate nsenumerator * enumerator = [mutabledic keyenumerator]; ID key = [enumerator nextobject]; while (key) {ID object = [mutabledic objectforkey: Key]; nslog (@ "% @", object); Key = [enumerator nectobject];}


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.