A summary of the usage of dictionary nsdictionary and Nsmutabledictionary

Source: Internet
Author: User

A friend who has done the Java language or C language development should be very clear about the keyword map, it can be stored in the form of key values, the value of the time through key can directly get the corresponding value, very convenient. In the Objective-c language, the Dictionary object is doing this, but in the same Dictionary object can hold many different types of data, unlike Java and C can only save the same type of data declared, Its key words are nsdictionary and nsmutabledictionary. The friends who read my previous article should be able to see the difference from the structure of the keyword. It is obvious that the former is an immutable dictionary or a mutable dictionary.

Nsdictionary *dict;

For (NSString * Akey in Dict)

{

//........

} Very useful

1. Create an immutable dictionary

[Nsdictionary Dictionarywithobjectsandkeys:..] : Creates a Dictionary object directly using the key-value pair, ending with the nil flag.

[Nsdictionary Initwithobjectsandkeys:..] : initializes the Dictionary object with the key-value pair, ending with the nil flag.

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

[Dictionary Keyenumerator]: stores all keys of the dictionary in Nsenumerator, Nsenumerator is much like an iterator in the Java language, using a quick enumeration to traverse all stored key values in the dictionary.

[Dictionary Objectenumerator]: stores all the value of the dictionary in Nsenumerator, and the usage and the above can be used to traverse the key corresponding to the stored value.

[Dictionary Objectforkey:key]: by passing in the key object can get the current key corresponding to the stored value.

#import <UIKit/UIKit.h>#import"MyClass.h"intMainintargcChar*argv[]) {NSAutoreleasePool*pool =[[NSAutoreleasePool alloc] init]; //Add our test codensdictionary*dictionary = [Nsdictionary Dictionarywithobjectsandkeys:@"Rain Pine Momo",@"name",@"15810463139",@" Number", nil]; //get the number of dictionaries    intCount =[dictionary Count]; NSLog (@"the number of dictionaries is:%d", Count); //get all the key values in the dictionaryNsenumerator * Enumeratorkey =[dictionary keyenumerator]; //Fast enumeration of values that traverse all keys     for(NSObject *Object inchEnumeratorkey) {NSLog (@"the value of traversing key:%@",Object); }        //get all value values in the dictionaryNsenumerator * Enumeratorvalue =[dictionary objectenumerator]; //Fast enumeration iterates through the values of all value     for(NSObject *Object inchenumeratorvalue) {NSLog (@"traversal value:%@",Object); }        //find value by keyNSObject *Object= [Dictionary Objectforkey:@"name"]; if(Object!=Nil) {NSLog (@"the value found through key is:%@",Object); }                intRetVal =Uiapplicationmain (argc, argv, nil, nil);    [Pool release]; returnRetVal;}

2. Create a variable Dictionary object

Nsmutabledictionary is a subclass of Nsdictionary, so it inherits the Nsdictionary method.

[Nsmutabledictionary Dictionarywithcapacity:10]: Create a mutable dictionary initially specifies that its length is 10. Dynamically add data if more than 10 this dictionary length will automatically increase , So don't worry about arrays being out of bounds. Recommended in this way

[Nsmutabledictionary Initwithcapacity:10]: Just initialize a dictionary with a length of 10.

[Dictionary setobject:@ "Rain Pine Momo" forkey:@ "name"]: Add data dynamically to a mutable dictionary, where the key is name and the value is Rain pine Momo. if the data for this key exists in the dictionary, the value of this key is replaced directly. (Easy to mix the place, cautious!) )

[Dictionary removeallobjects ...] : Removes all data from the dictionary.

[Dictionary Removeobjectforkey ...] : Deletes the data from the specified key in the dictionary.

#import <UIKit/UIKit.h>
#import "MyClass.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

Add our test code

Create a Dictionary object with an initialization length of 10
Nsmutabledictionary *dictionary = [Nsmutabledictionary dictionarywithcapacity:10];

Add data to a dictionary dynamically
[Dictionary setobject:@ "Rain Pine Momo" forkey:@ "name"];

[Dictionary setobject:@ "15810463139" forkey:@ "number"];


Find Value by key
NSObject *object = [Dictionary objectforkey:@ "name"];

if (Object! = nil) {
NSLog (@ "value found through key is:%@", object);
}



int retVal = Uiapplicationmain (argc, argv, nil, nil);
[Pool release];
return retVal;
}

Dictionary class exists in order to solve in a large number of data to find convenient, because it is directly through the key to find value so fast, to avoid a traversal of the inefficiency caused by the use of dictionary classes will help you speed up the program.

Article Source: http://blog.csdn.net/xys289187120/article/details/682373

A summary of the usage of dictionary nsdictionary and Nsmutabledictionary

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.