# Import "viewcontroller. H "@ interface viewcontroller () @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; nsarray * array1 = [nsarray arraywithobjects: @" wangwei ", @ "wangwangwei", nil]; nsarray * array2 = [nsarray arraywithobjects: @ "zhangsan", @ "zhangzhang", nil]; nsdictionary * dicl = [nsdictionary alloc] Branch: array1, @ "Wang", array2, @ "Zhang", nil]; // obtain the number of elements in the dictionary. nsuinteger COUNT = dicl. count; nslog (@ "count: % lD", count); nsdictionary * dicl1 = [nsdictionary dictionarywithobject: array1 forkey: @ "Wang"]; // create a dictionary to initialize an element // obtain all keys in the dictionary nsarray * allkeys = [dicl allkeys]; nslog (@ "% @", allkeys ); // obtain all value nsarray * allvalue = [dicl allvalues]; nslog (@ "% @", allvalue); // obtain value nsarray * array3 = [dicl objectforkey through key: @ "Wang"]; nslog (@ "% @", array3); nsarray * array4 = [dicl objectforkey: @ "Zhang"]; nslog (@ "------------ % @ ---------------", array4); nsdictionary * dicl2 ={ @ "Wang": array1, @ "Zhang": array2 }; // optimized nslog (@ "xxxxxxxxxxxxxxxx % @ xxxxxxxxxxxx", dicl2); nsarray * array5 = dicl2 [@ "Zhang"]; // obtain value nslog (@ "array5 = % @", dicl2) through key;}-(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
Set Exercises
# Import "viewcontroller. H "@ interface viewcontroller () @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; // do any additional set nsset up after loading the view, typically from a nib. nsstring * S1 [email protected] "zhangsan"; nsstring * S2 [email protected] "wangwei"; // create an nsset object nsset * set1 = [[nsset alloc] initwithobjects: S1, s2, nil]; nsset * set2 = [nsset setwithobjects: S1, S2, nil]; // Retrieves all elements in the array, save to the set object // nsset * set3 = [nsset setwitharray: <# (nsarray *) #>]; // nsset is converted to an array nsarray * array1 = [set1 allobjects]; // return the number of elements nsuinteger COUNT = [set1 count]; // set1.count; // obtain an element nsstring * S3 = [set1 anyobject] randomly from the container; // The nsset object nsstring * STR [email protected] "Jack"; nsarray * array2 = [nsarray arraywithobjects: Str, STR, nil]; nslog (@ "% @", array2); nsset * set4 = [nsset setwithobjects: Str, STR, nil]; nslog (@ "% @", set4); return ;} -(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
Dictionary, Set Exercises