Nsdictionary initialization and some common methods

Source: Internet
Author: User

Nsdictionary

// Initialize 1.

Nsdictionary * dic11 = [[nsdictionary alloc] initwithobjects: @ [@ "qwe", @ "ASD", @ "zxc", @ "QAZ", @ "wsx"] forkeys: @ [@ "111", @ "222", @ "333", @ "444", @ "555"];

// Obtain all the keys and values respectively.

Nslog (@ "% @", dic11.allkeys );

Nslog (@ "% @", dic11.allvalues );

// Obtain the total number of dictionary key-value pairs

Nslog (@ "% d", (dic11.count ));

// Dictionary traversal and quick Enumeration

For (ID key in dic11 ){

ID value = [dic11 objectforkey: Key];

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

}

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

Nsdictionary * [email protected] {@ "Keya": @ "AAA", @ "keyb": @ 111 };

Nslog (@ "% @", dic00 );

 

 

// Other initialization Methods

// Initialization of a key-Value Pair dictionary

 

Nsdictionary * dic1 = [nsdictionary dictionarywithobject: @ "10001" forkey: @ "000"];

 

// Initialize dictionary with the same content as dic1

 

Nsdictionary * dic0 = [nsdictionary dictionarywithdictionary: dic1];

 

// Initialize the dictionary with multiple key-value pairs

Nsdictionary * dic2 = [nsdictionary dictionarywithobjects: @ [@ "11", @ "22", @ "33", @ "44"] forkeys: @ [@ "first ", @ "second", @ "third", @ "four"];

 

 

// Initialize the dictionary with multiple key-value pairs 2

Nsdictionary * dic3 = [nsdictionary dictionarywithobjectsandkeys ", @ "Xiang zhiduo", @ "tease the Holy Buddha", nil];

 

Nslog (@ "% @", dic3 [@ ""]);

 

// Create a variable dictionary with the same content as dic2

Nsmutabledictionary * dic4 = [nsmutabledictionary dictionarywithdictionary: dic2];

 

 

// Insert a key-value pair with the same name to overwrite the original value

[Dic4 setobject: @ "jjj" forkey: @ "Jiji"];

Nslog (@ "% @", dic4 );

 

// Insert a key-value pair with the same name to overwrite the original value

[Dic4 setobject: @ "999" forkeyedsubscript: @ "Lan"];

[Dic4 setobject: @ "004" forkeyedsubscript: @ "four"];

Nslog (@ "% @", dic4 );

// Splice dic1 to the front of DIC

[Dic4 addentriesfromdictionary: dic1];

Nslog (@ "% @ ------", dic4 );

// Remove a key-Value Pair

[Dic4 removeobjectforkey: @ "third"];

Nslog (@ "% @", dic4 );

// Remove the key-value pair corresponding to the key in the array

[Dic4 removeobjectsforkeys: @ [@ "five", @ "000"];

Nslog (@ "% @", dic4 );

// Replace all the original content of dic4 with the content of dic11.

[Dic4 setdictionary: dic11];

Nslog (@ "% @", dic4 );

// Save the dictionary content to the file in the specified path

[Dic4 writetofile: @ "/users/Apple/desktop/dic4.plist" atomically: Yes];

// Read the file content from the specified path

Nsdictionary * dic5 = [nsdictionary dictionarywithcontentsoffile: @ "/users/Apple/desktop/dic4.plist"];

Nslog (@ "% @", dic5 );

// Dict3 and dict2 have the same content and are essentially a new object.

// Dictionary is an initialization method for retrieving content from a specified path file. The file must exist. Otherwise, the content is empty.

Nsdictionary * dic6 = [[nsdictionary alloc] initwithcontentsoffile: @ "/users/Apple/desktop/dic4.plist"];

Nslog (@ "% @", dic6 );

 

// Remove all key-value pairs.

[Dic4 removeallobjects];

Nslog (@ "% @", dic4 );

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.