What about dictionary objects in objective-C syntax (8)

Source: Internet
Author: User

Objective-CGrammar-something about dictionary objects

If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/421


DoneJavaLanguageOrCLanguageDevelopers should be clearKeywordsMapIt can store data in the form of key-value pairs.KeyYou can directly obtain the corresponding value, which is very convenient. InObjective-CLanguageThe dictionary object does this, but the same dictionary object can store multiple different types of data, unlikeJavaAndCOnly declared data of the same type can be saved. Its keyword isNsdictionaryAndNsmutabledictionary. Friends who have read my previous articles should see the difference between the two from the keyword structure. Obviously, the former is an unchangeable dictionary or a mutable dictionary.

1.Create an unchangeable dictionary

[Nsdictionary dictionarywithobjectsandkeys: ..]:Use Key-value pairs to directly create dictionary objects.NilThe flag ends.

[Nsdictionary initwithobjectsandkeys: ..]:Use Key-value pairs to initialize dictionary objects. It must endNilThe flag ends.

[Dictionary count]:The length unit of the dictionary.

[Dictionary keyenumerator]:Convert allKeyStored inNsenumeratorMedium,NsenumeratorSimilarJavaLanguageYou can use Quick enumeration to traverse all the buckets in the dictionary.KeyValue.

[Dictionary objectenumerator]:Convert allValueStored inNsenumeratorMedium,Similar to the preceding methodKeyCorresponding StorageValueValue.

[Dictionary objectforkey: Key]:Pass inKeyThe object can get the currentKeyThe storage value.



# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code nsdictionary * dictionary = [nsdictionary dictionarywithobjectsandkeys: @ "Yu Song Momo", @ "name", @ "15810463139", @ "Number", nil]; // obtain the number of dictionaries int COUNT = [dictionary count]; nslog (@ "Number of dictionaries: % d", count ); // obtain all the key values in the dictionary. nsenumerator * enumeratorkey = [dictionary keyenu Merator]; // quickly enumerate all key values for (nsobject * object in enumeratorkey) {nslog (@ "traversal key value: % @", object );} // obtain all value values in the dictionary nsenumerator * enumeratorvalue = [dictionary objectenumerator]; // quickly enumerate all values for (nsobject * object in enumeratorvalue) {nslog (@ "traversal value: % @", object);} // locate value nsobject * object = [dictionary objectforkey: @ "name"] through key; if (object! = Nil) {nslog (@ "the value found through key is: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil ); [pool release]; return retval ;}

2.Create a variable dictionary object

NsmutabledictionaryYesNsdictionarySo it inheritsNsdictionary.

[Nsmutabledictionary dictionarywithcapacity: 10]:Create a variable dictionary and specify its length10.Dynamically add data if it exceeds10The length of this dictionary is automatically increased, so you don't have to worry about array out-of-bounds.

[Nsmutabledictionary initwithcapacity: 10]:Only initialize the length of a dictionary10.

[Dictionary setobject :@"Yu SongMomo "forkey: @" name "]:Dynamically add data to a variable dictionary, HereKeyYesNameThe value is Yusong.Momo. If this exists in the dictionaryKeyDirectly replaceKey.

[Dictionary removeallobjects ..]:Delete all data in the dictionary.

[Dictionary removeobjectforkey...]:Delete the specifiedKeyData.

# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code // create a dictionary object, the initialization length is 10 nsmutabledictionary * dictionary = [nsmutabledictionary dictionarywithcapacity: 10]; // dynamically add data to the dictionary [dictionary setobject: @ "Yu Song Momo" forkey: @ "name"]; [dictionary setobject: @ "15810463139" forkey: @ "Number"]; // locate value nsobject * object = by key [Dictionary objectforkey: @ "name"]; If (object! = Nil) {nslog (@ "the value found through key is: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil ); [pool release]; return retval ;}

The existence of the dictionary class is to facilitate searching for a large amount of data, because it usesKeyFindValueSo the speed is very fast, avoiding the inefficiency caused by traversal and searching one by one. making good use of the dictionary class will help your program speed up.Wow ~ Good night, friends ~

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.