1224 Dictionary Dictionary

Source: Internet
Author: User
Tags allkeys delete key

Immutable dictionary Nsdictionary

Dictionaries have no order, key-value pairs

Key-value

Key (String object)-Value (object)

0. The values of multiple key-value pairs can be the same in a dictionary, but the keys cannot be the same and must be unique

1. Advantages of dictionary Storage

A. Easy to find

B. Easy to hack

2. How to create a dictionary

Nsdictionary *dic = @{@ "Xiao Wang": xw,@ "Zhang San": zs,@ "ls": ls};

Nsdictionary *dic1 = [Nsdictionary dictionary];

Nsdictionary *dic1_ = [[Nsdictionary alloc]init];

Nsdictionary *dic2 = [Nsdictionary dictionarywithdictionary:dic];

Nsdictionary *dic2_ = [[Nsdictionary alloc]initwithdictionary:dic];

Initializes a key-value pair

Nsdictionary *dic3 = [nsdictionary dictionarywithobject:xw forkey:@ "Xiao Wang"];

Create a dictionary with multiple key-value pairs

Nsdictionary *dic4 = [nsdictionary Dictionarywithobjects:@[xw,zs,ls] forkeys:@[@ "Xiaowang", @ "Zhangsan", @ "Lisi"];

3. Get the number of dictionary key-value pairs

NSLog (@ "Count:%ld", Dic5.count);

4, get all keys keys (String object)

Nsarray*allkeys = [Dic5 AllKeys];

5, get all the value

Nsarray *allvalues = [Dic5 allvalue];

6. Traverse a dictionary

For (NSString *key in AllKeys) {

NSLog (@ "key =%@:value =%@", key,[dic5 Objectforkey:key]);

}

7, get the corresponding value by key

Nsarray *temparray = [dic5 objectforkey:@ "Man"]);

Nsmubtabledictionary

1. Creating a mutable Dictionary

Nsmutabledictionary *mdic = [Nsmutabledictionary dictionarywithcapacity:0];

2. Add a key-value pair

[MDic setobject:@ "YQL" forkey:@ "name"];

[MDic setobject:parray forkey:@ "man"];

[MDic setobject:@ "Red Mansions" forkey:@ "classics"];

NSLog (@ "7.%@", mDic);

3. Change the value of a key

[MDic setobject:@ "Hongloumeng" forkey:@ "classics"];

4. Delete key-value pairs

[MDic removeobjectsforkeys:@ "Classics"];

[MDic removeobjectsforkeys:@[@ "man", @ "NB", @ "Hel"];

[MDic removeallobjects];

NSLog (@ "8..%@", mDic);

1224 Dictionary Dictionary

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.