Set-Dictionary Method exercises

Source: Internet
Author: User

-(Void) viewdidload {

[Super viewdidload];

// Initialize a dictionary object with one or more key-value pairs in the order of values, keys, values, keys, keys,..., and nil

Nsdictionary * dict = [[nsdictionary alloc] initwithobjectsandkeys: @ 1, @ "key1", @ 2, @ "key2", @ 3, @ "key3", nil];

Nslog (@ "dict: % @", dict );

// Use two arrays to initialize a dictionary (one key array and one value array, which must be of the same size; otherwise, the dictionary will crash.

Nsarray * valuearray = @ [@ "1", @ "2", @ "3"];

Nsarray * keyarray = @ [@ "key1", @ "key2", @ "key"];

Nsdictionary * dict2 = [[nsdictionary alloc] initwithobjects: valuearray forkeys: keyarray];

Nslog (@ "dict: % @", dict2 );

Nsdictionary * dict3 = [[nsdictionary alloc] initwithdictionary: dict];

Nslog (@ "dict: % @", dict3 );

// Save the dictionary object content to the file

Nsstring * Path = @ "/users/Apple/desktop/dict. plist ";

[Dict writetofile: path atomically: Yes];

// Read data from the previously saved file to the dictionary object

Nsdictionary * dict4 = [nsdictionary dictionarywithcontentsoffile: path];

Nslog (@ "dict4: % @", dict4 );

// You can initialize a literal constant like an immutable dictionary. The key is in the front and the value is in the back.

Nsdictionary * dict5 =@ {@ "key": @ 1, @ "key1": @ 2, @ "key3": @ 3 };

Nslog (@ "dict5: % @", dict5 );

// Obtain the corresponding object using the specified key. If this key is not found, Nil is returned.

Nsnumber * num = [dict objectforkey: @ "key"];

Nslog (@ "Num: % @", num );

// Obtain the number of key-value pairs in the dictionary.

Nsinteger paircount = [dict count];

Nslog (@ "paircount: % d", paircount );

Nsarray * allkeys = [dict allkeys];

Nsarray * allvalues = [dict allvalues];

Nslog (@ "allkeys: % @", allkeys );

Nslog (@ "allvalue: % @", allvalues );

// Old Traversal method

Nsarray * allkeys2 = [dict2 allkeys];

Nsuinteger COUNT = [allkeys2 count];

For (INT I = 0; I <count; I ++ ){

ID key = [allkeys2 objectatindex: I];

ID value = [dict2 objectforkey: Key];

Nslog (@ "% @: % @", key, value );

}

// Quick Enumeration

For (ID key in dict2 ){

ID value = [dict objectforkey: Key];

Nslog (@ "% @: % @", key, value );

}

// Immutable dictionary object

Nsmutabledictionary * mdict = [[nsmutabledictionary alloc] initwithobjectsandkeys: @ 1, @ "key", @ 2, @ "key2", nil];

// Delete a key-Value

[Mdict removeobjectforkey: @ "key"];

Nslog (@ "mdict: % @", mdict );

// Add a key-Value

[Mdict setobject: @ 3 forkey: @ "key3"];

Nslog (@ "mdict: % @", mdict );

Nsmutabledictionary * mdict2 = [[nsmutabledictionary alloc] initwithobjectsandkeys: @ 1, @ "key4", @ 2, @ "key4", @ 3, @ "key5", nil];

// Add the key-value in a dictionary object to another dictionary object

[Mdict2 addentriesfromdictionary: mdict];

Nslog (@ "mdcit2: % @", mdict2 );

[Mdict removeallobjects];

Nslog (@ "mdict: % @", mdict );

Nsmutabledictionary * mdict3 = [[nsmutabledictionary alloc] initwithobjectsandkeys: @ 1, @ "key3", @ 2, @ "key4", @ 3, @ "key5", nil];

Nsmutabledictionary * mdict4 = [[nsmutabledictionary alloc] initwithobjectsandkeys: Nil];

[Mdict4 setdictionary: mdict3];

Nslog (@ "mdict4: % @", mdict4 );

Nsarray * array = @ [@ "key4", @ 2, @ "key4", @ 3, @ "key5"];

[Mdict3 removeobjectsforkeys: array];

// Set

Nsset * Set = [[nsset alloc] initwithobjects: @ 1, @ 2, @ 3, nil];

Nsset * set2 = [[nsset alloc] initwithobjects: @ 3, @ 4, @ 5, nil];

// Randomly retrieve an element

Nsnumber * num2 = [set anyobject];

Nslog (@ "num2: % @", num2 );

// Determine whether the set contains an element

Bool B = [set containsobject: @ 1];

Nslog (@ "% HHD", B );

// Determine whether two sets have an intersection

Bool a = [set intersectsset: set2];

Nslog (@ "% HHD", );

// Determine whether the two sets are equal

Bool c = [set isequaltoset: Set];

Nslog (@ "% HHD", C );

// Determine whether a set is a subset of Two Sets

Bool d = [set issubsetofset: set2];

Nslog (@ "% HHD", d );

// Immutable set

Nsmutableset * mset = [[nsmutableset alloc] initwithobjects: @ 1, @ 2, @ 3, nil];

Nsmutableset * mset2 = [[nsmutableset alloc] initwithobjects: @ 2, @ 3, nil];

// [Mset intersectsset: mset2];

// [Mset addobject: @ 4];

// [Mset removeobject: @ 4];

// Nsarray * array3 = @ [@ 4, @ 5];

// [Mset addobjectsfromarray: array3];

// [Mset minusset: mset2];

// [Mset removeallobjects];

[Mset unionset: mset2];

Nslog (@ "% @", mset );

 

 

}

 

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.