Iosoc immutable dictionaries and mutable dictionaries

Source: Internet
Author: User

Both key and value belong to the (ID) object type

Key commonly used string nsstring to represent

Store numeric type generally available nsstring

int age->@ (age)

[Dic[name] intvalue];

@ ()-Turn into NSNumber type

Intvalue

Value can make any object (NSString Nsarrray, dog)

Nsdictionary once created cannot modify add delete

#pragma the creation of the Mark dictionary

Nsdictionary * dic = [[Nsdictionary Alloc]initwithobjectsandkeys:

@ "value1", @ "Key1",

@ "value2", @ "Key2",

@ "value2", @ "Key3", nil];

NSLog (@ "%@", DIC);

Nsdictionary * Dic1 = [nsdictionary dictionarywithobjectsandkeys:@ "value1", @ "Key1", nil];

Create a dictionary with a dictionary

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

NSLog (@ "%@", Dic2);

xcode4.6

Format: Key:value between multiple elements, separated by

Using NSLog to print a dictionary is a transcoding that does not support Chinese characters

\u5bd2\u4ed2;

Nsdictionary * dic3 =

@{@ "Key1": @ "value1",

@ "Key2": @ "value2",

@ "Key3": @ "Value3",

@ "Key4": @ "value2"

};

#pragma mark to see the number of keys

Nsuinteger num = [Dic2 count];

NSLog (@ "%lu", num);

#pragma mark to find value by key

NSString * str = [dic3 objectforkey:@ "Key1"];

xcode4.6

NSString * str2 = dic3[@ "Key2"];

NSLog (@ "%@", str);

NSLog (@ "%@", str2);

#pragma mark gets all keys all value

Nsarray * Keyarray = [dic3 AllKeys];

Nsarray * ValueArray = [dic3 allvalues];

NSLog (@ "keys =%@", Keyarray);

NSLog (@ "values =%@", ValueArray);

#pragma mark to view a value that corresponds to multiple keys

Nsarray * array = [dic3 allkeysforobject:@ "value2"];

NSLog (@ "%@", array);

#pragma mark Variable Dictionary

#pragma mark to create a mutable dictionary

Nsmutabledictionary * Mutdic = [[Nsmutabledictionary alloc] init];

Nsdictionary * Dic4 =

@{@ "Key1": @ "value1",

@ "Key2": @ "value2",

@ "Key3": @ "Value3",

@ "Key4": @ "value2"

};

Nsmutabledictionary * MutDic2 = [[Nsmutabledictionary alloc]initwithdictionary:dic4];

#pragma Mark added

Key not repeating is to increase the value of the key pair

[MutDic2 setobject:@ "Value3"

forkey:@ "Key5"];

Ensure key does not repeat key must be unique

Whether obj is ==null by querying the key

if ([MutDic2 objectforkey:@ "Key5"]== NULL) {

//

//        }

Nsdictionary * dic5 = @{@ "key_1": @ "value1",

@ "key_2": @ "value2"};

[MutDic2 ADDENTRIESFROMDICTIONARY:DIC5];

NSLog (@ "%@", MutDic2);

#pragma mark Delete

Delete a value by key

[MutDic2 removeobjectforkey:@ "Key_1"];

NSLog (@ "%@", MutDic2);

Nsarray * array2 = @[@ "key_2", @ "Key2", @ "Key4"];

[MutDic2 Removeobjectsforkeys:array2];

NSLog (@ "%@", MutDic2);

[MutDic2 removeallobjects];

NSLog (@ "%@", MutDic2);

#pragma mark changes

Key already exists is the value that modifies the current value

[MutDic2 setobject:@ "HHHH" forkey:@ "Key1"];

NSLog (@ "%@", MutDic2);

Completely overwrite all values in the current dictionary

[MutDic2 Setdictionary:dic2];

NSLog (@ "%@", MutDic2);

}

Iosoc immutable dictionaries and mutable dictionaries

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.