Simple operation of Nsdictionary and Nsset in OC

Source: Internet
Author: User

/**

* Dictionary

data that holds the key-value pair type

storing data is unordered.

*/

dictionary output in console is included with {} .

Nsdictionary Immutable Dictionary

1. Creating Objects

Initialize Method

Nsdictionary *dict1 = [[nsdictionary alloc] initwithobjectsandkeys:@ "Zhizunbao" , @ "name", @ "man", @ "Sex", @27, @ "age", Nil];

NSLog(@ "%@", Dict1);

Nsdictionary *dict2 = [[nsdictionary alloc] initwithobjects:@[@ "Yinhao", @ "Man", @27] forkeys:@[@ "name" @ "Sex" @ "age"] ];

NSLog(@ "%@", dict2);

class Method

Nsdictionary *dict3 = [nsdictionary dictionarywithobjectsandkeys:@ "LeBron" @ "name" @ "Man ", @ "Sex", @31, @ "age", Nil];

NSLog(@ "%@", dict3);

method of literal weight

Nsdictionary *dict4 = @{

@ "Name" : @ "LeBron",

@ "Sex" : @ "man",

@ "Age" : @32,

@ "Hobby" : @ "basketball",

};

NSLog (@ "%@", Dict4);

2. Find

Find object based on key

NSString *string1 = [Dict4 objectforkey:@ "name"];

NSLog (@ "%@", string1);

Subscript Method

NSLog (@ "%@", dict4[@ "name"]); // Two ways to do the same

3. get the length of the dictionary

NSLog (@ "%lu", [Dict4 Count]);

NSLog (@ "%lu", Dict4.count);

Nsmutabledictionary Variable Dictionary

The key in the dictionary is unique, and if a set method is used to add an element with the same name as a key in the dictionary , the value inside will be replaced, that is , if the dictionary does not have a key an element with the same name will be added;

1. Create an Object ( because it is a subclass of Nsdictionary, so there are nsdictionary methods,nsmutabledictionary have )

You can create an empty mutable dictionary

Nsmutabledictionary *mdict1 = [nsmutabledictionary dictionarywithcapacity:4];

of course, you can also create a dictionary based on existing dictionaries.

Nsmutabledictionary *mdict2 = [Nsmutabledictionary dictionarywithdictionary:dict4];

NSLog (@ "%@", mDict2);

2. Add, modify key-value pairs

[MDict1 addentriesfromdictionary:@{@ "height" : @ "203", @ "Weight" : @ "+"}] ;

NSLog (@ "%@", MDict1);

[MDict2 setobject:@ "203" Forkey:@ "height"];

NSLog (@ "%@", mDict2);

[MDict2 setValue:@ "America" Forkey:@ "Address"];

NSLog (@ "%@", mDict2);

/*

Setvalue:forkey: The key parameter type must be NSString * type, which is the method of NSObject object, which means that all OC Object has this method

Setobject:forkey: The key parameter type can be any type of object and is a unique method in the Nsmutabledictionary class

*/

3. Delete

Delete based on key

[MDict2 Removeobjectforkey:@ "name"];

Delete based on multiple keys

[MDict2 Removeobjectsforkeys:@[@ "hobby", @ "height"];

Delete all

[MDict2 removeallobjects];

NSLog (@ "%@", mDict2);

/**

* Nsset Immutable collection (elements cannot be duplicated, if there are multiple, only one is output, and is stored in an unordered order)

*/

Create a set object

Nsset *set1 = [Nsset setwithobjects:@ "Genie", @ "Weizhuang", @ "tianming", @ "Shaoyu", Nil];

NSLog (@ "%@", Set1);

arbitrarily remove an element ( not random )

NSString *str = [Set1 anyobject];

NSLog (@ "%@", str);

Remove all elements

Nsarray *sarray = [Set1 allobjects];

NSLog (@ "%@", Sarray);

gets the number of elements in the collection

NSLog (@ "%lu", Set1.count);

determines whether the object is in the collection

BOOL Ishasshaoyu = [Set1 containsobject:@ "Shaoyu"];

NSLog (@ "%d", Ishasshaoyu);

Nsmutableset Variable Set

Creating Objects

Nsmutableset *mset1 = [Nsmutableset setwithobjects:@ "Genie", @ "Weizhuang", @ "tianming", @ "Shaoyu", Nil];

NSLog (@ "%@", MSet1);

adding Objects

[MSet1 AddObject:@ "Gaoyue"];

NSLog (@ "%@", MSet1);

Delete Object

[MSet1 removeobject:@ "Shaoyu"];

NSLog (@ "%@", MSet1);

Simple operation of Nsdictionary and Nsset in OC

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.