Objective-c nsdictionary immutable dictionaries and nsmutabledictionary mutable dictionaries

Source: Internet
Author: User
Tags allkeys

Create an immutable Dictionary object

// Facilitation Builder Creation

nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:@ "Zhonger", @ "name2", @ "Taixu", @ "name1", @ "Caicai", @ "Name3", Nil] ;

// Initialize Method

//Give value to key first

nsdictionary *dic1 = [[nsdictionary alloc]initwithobjectsandkeys:@ "Zhonger" @ " name2", @ " taixu", @ "name1", @ "Caicai", @ "Name3", Nil ];

nsdictionary *dic2 = [[nsdictionary alloc] initwithobjects:@[@ "name1" , @ "name2", @ "Name3"] forkeys:@[@ "Zhonger", @ "name2", @ "Taixu"];

// laugh grammar

//key in front of value in the rear

nsdictionary *dic3 = @{@ "name1":@ "Zhanger" @ " name2":@ "Caicai" @ "Name3":@ "Fufu"};

// Get the number of key-value pairs in the dictionary, and get all the keys and all the value;

Nsuinteger count = [dic3 count];

nsarray *arr = [dic3 allKeys];

NSLog(@ "dic3 AllKeys is%@", arr);

NSLog(@ "dic3 Allvalue is%@", [dic3 allvalues]);

// Gets a value in the dictionary , via key

nsstring *string = [dic3 objectforkey:@ "name2"];

nsarray *allkeys = [dic3 allkeys];

for (int i = 0; i < [AllKeys count]; i + +) {

NSLog(@ "%@", [dic3 objectforkey: allkeys[i]]);

}

// Create variable Dictionary object, initialize method, convenience constructor, literal

nsmutabledictionary *mdic = [[nsmutabledictionary alloc] initwithcapacity:0 ];

nsmutabledictionary *mdic1 = [nsmutabledictionary dictionarywithcapacity:0 ];

nsmutabledictionary *mdic2 = [@{@ "Key1":@ "Zhangsan" @ "Key2":@ "Lisi" @ "Key3":@ "Wangwu"}mutablecopy];

NSLog(@ "MDic2 is%@", MDic2);

// increase the key-value pair method Setobject:forkey:

[MDic2 setobject:@ "Liumazi" forkey:@ "Key4"];

NSLog(@ "MDic2 is%@", MDic2);

// Delete the key and value pairs, and after deletion , both key and value do not exist.

//removeobjectforkey:

[MDic2 Removeobjectforkey:@ "Key2"];

NSLog(@ "MDic2 is%@", MDic2);

// Remove all key-value pairs

[MDic2 removeallobjects];

NSLog(@ "MDic2 is%@", MDic2);

The method of modifying key-value pairs is the same as adding key-value pairs.

[MDic2 setobject:@ "Zhangsan" forkey:@ "Key1"];

NSLog(@ "MDic2 is%@", MDic2);

[MDic2 setobject:@ "Lisi" forkey:@ "Key1"];

NSLog(@ "MDic2 is%@", MDic2);

//The Key in the dictionary cannot be duplicated , but value can be repeated, and the dictionary is an unordered collection

Objective-c nsdictionary immutable dictionaries and nsmutabledictionary mutable dictionaries

Related Article

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.