Dictionary, Set

Source: Internet
Author: User
Nsdictionary * dictionary = [[nsdictionary alloc] initwithobjectsandkeys: @ "value1", @ "key1", @ "value2", @ "key2", @ "V3 ", @ "K3", @ "V5", @ "K5", @ "V4", @ "K4", @ "value6", @ "key6", @ "value7 ", @ "ke7", nil]; nslog (@ "% @", dictionary); // create nsdictionary * dictionary2 = [nsdictionary dictionarywithobjectsandkeys: @ "V1 ", @ "K1", @ "V2", @ "K2", nil]; nslog (@ "% @", dictionary2); nsdictionary * dictionary3 = [nsdictionary dictionarywithobject: @ "value" forkey: @ "key"]; nslog (@ "% @", dictionary3); nsarray * keyarray = [nsarray arraywithobjects: @ "K1 ", @ "K2", @ "K3", nil]; // nsarray * valuearray = [nsarray arraywithobjects: @ "V1", @ "V2 ", @ "V3", nil]; // nsdictionary * dictionary4 = [nsdictionary dictionarywithobjects: keyarray forkeys: valuearray]; nslog (@ "% @", dictionary4 ); // create an empty dictionary nsdictionary * DIC = [nsdictionary dictionary]; // create a dictionary object using a file: command + N-> resource-> property listnsdictionary * dictionary5 = [nsdictionary dictionarywithcontentsoffile: @ "/users/lanou3g/desktop/OC/lesson5-20140425/lesson5-20140425/dictionary. plist "]; nslog (@" % @ ", dictionary5); // return the number of key-value pairs in the dictionary nslog (@" % lD ", [dictionary5 count]); // obtain the valuensstring * value1 = [dictionary5 objectforkey: @ "ly"]; nslog (@ "% @", value1 ); // return all key arrays nsarray * allkeysarray = [dictionary5 allkeys]; nslog (@ "% @", allkeysarray ); // return all value arrays nsarray * allvaluesarray = [dictionary5 allvalues]; nslog (@ "% @", allvaluesarray); // use the key enumerator (get all keys) nsenumerator * enumerator = [dictionary5 keyenumerator]; nsstring * STR = nil; while (STR = [enumerator nextobject]) {nslog (@ "% @", STR );} 4.2 create a variable dictionary nsmutabledictionarynsmutabledictionary * dic1 = [nsmutabledictionary dictionarywithobjectsandkeys: @ "V1", @ "K1", @ "V2", @ "K2", nil]; nsmutabledictionary * dic2 = [nsmutabledictionary dictionarywithobjectsandkeys: @ "V3", @ "K3", @ "V4", @ "K4", @ "V5", @ "K5 ", nil]; // used to splice objects [dic1 addentriesfromdictionary: dic2]; nslog (@ "% @", dic1); // delete an object in the dictionary [dic1 removeobjectforkey: @ "K1"]; nslog (@ "% @", dic1); // delete all dictionary objects [dic1 removeallobjects]; nslog (@ "% @", dic1 ); // set the dictionary [dic1 setdictionary: dic2]; nslog (@ "% @", dic1); 5.1nsset collection object container class // 1. use the class method to create an object nsset * set1 = [nsset set]; // create an empty set object nsset * set2 = [nsset setwithobject: @ "ABC"]; nsset * set3 = [nsset setwithobjects: @ "ABC", @ "AAA", @ "BBB", nil]; nslog (@ "% @", set3 ); nsarray * array = [nsarray arraywithobjects: @ "A", @ "B", @ "C", nil]; nsset * set4 = [nsset setwitharray: array]; // use an array to create nslog (@ "% @", set4); nsset * set5 = [nsset setwithset: set4]; // use a collection to create nslog (@ "% @", set5); // 2. create nsset * set6 = [[nsset alloc] init]; nslog (@ "% @", set6); nsset * set7 = [[nsset alloc] initwithobjects: @ "hello", @ "HHaa", @ "bbjdh", nil]; nslog (@ "% @", set7); nsset * set8 = [[nsset alloc] initwitharray: array]; nslog (@ "% @", set8); nsset * set9 = [[nsset alloc] initwithset: set7]; nslog (@ "% @", set9 ); // 3. returns the number of elements nslog (@ "% lD", [set7 count]); // 4. nsenumerator * enumerator = [set7 objectenumerator]; nsstring * STR = nil; while (STR = [enumerator nextobject]) {nslog (@ "% @", str);} // 5. determine whether two of them have an intersection bool ifhasintersection = [set2 intersectsset: set3]; nslog (@ "% d", ifhasintersection); // 6. determine whether two sets are equal nslog (@ "% d", [set2 isequaltoset: set3]); // 7. determine whether the current collection is a subset nslog (@ "% d", [set2 issubsetofset: set3]); 5.2 variable set nsmutableset // create a set object nsmutableset * mutableset = [nsmutableset setwithcapacity: 4]; [mutableset addobject: @ "AAA"]; nslog (@ "% @", mutableset); // class method to create a variable set nsmutableset * mutableset1 = [nsmutableset setwithobjects: @ "AAA", @ "BBB ", @ "CCC", nil]; nsmutableset * mutableset2 = [nsmutableset setwithobject: @ "AAA"]; // Add an object to the set [mutableset2 addobject: @ "DDD"]; nslog (@ "% @", mutableset2); // delete an object from the set [mutableset2 removeobject: @ "DDD"]; nslog (@ "% @", mutableset2 ); // Add the array object to the set object nsarray * arr = [nsarray arraywithobjects: @ "eee", @ "fff", nil]; [mutableset1 addobjectsfromarray: arr]; nslog (@ "% @", mutableset1); // get the intersection of the two sets. Note the difference between intersectset and intersectsset. The latter is the method to determine whether there is an intersection, the returned bool value [mutableset1 intersectset: mutableset2]; nslog (@ "% @", mutableset1); // subtract another set from one set [mutableset1 minusset: mutableset2]; nslog (@ "% @", mutableset1); // delete all elements from an element [mutableset2 removeallobjects]; nslog (@ "% @", mutableset2 ); // obtain the union of the two sets [mutableset1 unionset: mutableset2]; nslog (@ "% @", mutableset1); nslog (@ "% @", mutableset1 ); // assign a value to the set [mutableset1 setset: mutableset2]; nslog (@ "% @", mutableset1); about array sorting nsarray * array = [nsarrayarraywithobjects: @ "12 ", @ "454", @ "7878", @ "77122", @ "555", @ "9", nil]; nslog (@ "Before sorting: % @", array); // This sorting is performed by string size, not by numerical values. array = [array sortedarrayusingselector: @ selector (compare :)]; nslog (@ "after sorting: % @ ", array); // The following method is required for numerical sorting: array = [array sortedarrayusingfunction: intsort context: NULL]; nslog (@" after sorting: % @", array); The following is the implementation of intsort: nsinteger intsort (ID num1, Id num2, void * context) {int V1 = [num1 intvalue]; int v2 = [num2 intvalue]; if (V1 <V2) returnnsorderedascending; else if (V1> V2) returnnsordereddescending; else returnnsorderedsame;} Arrays can be stored in the dictionary and array // dictionary, the array can be saved into the dictionary nsarray * arr = [nsarray arraywithobjects: @ "1", @ "2", @ "3", nil]; // put the array ARR in the dictionary DIC. nsdictionary * DIC = [nsdictionary dictionarywithobject: arr forkey: @ "array"]; nslog (@ "% @", DIC ); // place the dictionary DIC And array ARR in array arr2. nsarray * arr2 = [nsarray arraywithobjects: arr, DIC, nil]; nslog (@ "% @", arr2 );

 

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.