The Dictionary of Objective-c

Source: Internet
Author: User
Tags allkeys

//Dictionary: (keyword value)

Insert code word is too small

Nsarray *array = [Nsarray array];//empty array

Nsdictionary *dictionary = [Nsdictionary dictionary];//empty Dictionary

Nsdictionary *my = [nsdictionary dictionarywithobject:@ "objective" forkey:@ "key"];

NSLog (@ "%@", my);

nsdictionary *to = [nsdictionary dictionarywithobjectsandkeys:@ "123", @ "abc", @ "456", @ "EFG", nil]; //Create a dictionary with multiple values

NSLog (@ "%@", to);

Nsdictionary *me = @{

@ "A": @ "1",

@ "B": @ "2"

};

NSLog (@ "%@,%li", Me,me.count);

NSString *s = [Me objectforkey:@ "a"];//key value corresponding to the object

NSString *ss = me[@ "B"];//is similar to an array

NSLog (@ "%@,%@", ss,s);

Nsarray *keyarr = [me AllKeys];

For (NSString *key in Keyarr)

//        {

NSLog (@ "%@ =%@", Key,me[key]);

//        }

Nsdictionary *niubi = [nsdictionary dictionarywithobjectsandkeys:@ "Liyuanfang", @ "Direnjie", @ "Moran", @ "Fuermosi", @ " Kenanfushou "@" Kenan ", nil];//create a dictionary with multiple values

Nsarray *keyarr = [Niubi allkeys];//takes all key values in the dictionary

For (NSString *key in Keyarr)

{

NSLog (@ "%@ asked%@ what do you think?" ", Key,niubi[key]);

}

/**********************************************************************************/

Take out all the values in the dictionary

Nsarray *valuearr = [me allvalues];

NSLog (@ "takes the value of all keys in the dictionary%@", Valuearr);

//Dictionary is present, then the developer must think of the convenience of using it

However, there are ways to remove keys or values independently: AllKeys and Allvalues

Because they are array properties, they need to be placed in the newly created array object

An efficient way to iterate through the OC language in the enumerator,

When applied, the general first passes through ..... Enumerator gets the enumerator that is stored in its corresponding type Nsenumerator object

This object will have an automatic down-traversal method Nextobject

Gets the enumerator for key in the dictionary, and then iterates through the enumerator to get the value corresponding to the key

Nsenumerator *e = [me keyenumerator];

ID obj;

while (obj = [E Nextobject]) {

NSLog (@ "%@ =%@", obj,me[obj]);

}

//Get enumerator for value

Nsenumerator *a = [me objectenumerator];

while (obj = [a nextobject])

{

NSLog (@ "%@", obj);

}

//Key and object enumeration block, stop represents traversal stop

[Me enumeratekeysandobjectsusingblock:^ (ID key, id obj, BOOL *stop) {

NSLog (@ "key=%@,value =%@", key,obj);

}];

The Dictionary of Objective-c

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.