At present, the most detailed and clear nsdictionary and Nsmutabledictionary Usage Summary _ meticulous and clear

Source: Internet
Author: User
Tags uikit

The Java language or C language development friends should be very clear key map bar, it can be the data in the form of the key to the child storage, when the value of the key can be directly to get the corresponding value, very convenient. Dictionary objects do this in the Objective-c language, but they can hold multiple different types of data in the same Dictionary object, unlike Java and C, which can only hold the same type of data as declared. Its key words are nsdictionary and nsmutabledictionary. 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 variable dictionary.

Nsdictionary *dict;

For (NSString * Akey in Dict)

{

//........

It works fine.

1. Create Immutable Dictionaries

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

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

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

[Dictionary Keyenumerator]: Store all keys in the dictionary in Nsenumerator, nsenumerator much like an iterator in the Java language, and use a quick enumeration to traverse all the stored key values in the dictionary.

[Dictionary Objectenumerator]: stores all the values of the dictionary in Nsenumerator, and the usage is almost as much as the value that can be used to traverse the key corresponding to the store.

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

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

Add our test code

Nsdictionary *dictionary = [Nsdictionary dictionarywithobjectsandkeys:@ "Rain pine Momo", @ "name", @ "15810463139", @ "number", NIL];

Get the number of dictionaries
int count = [Dictionary count];
The number of NSLog (@ "Dictionary is:%d", count);

Get all the key values in the dictionary
Nsenumerator * Enumeratorkey = [dictionary keyenumerator];

Quickly enumerate through the values of all key
For (NSObject *object in Enumeratorkey) {
NSLog (@ "Traverse key value:%@", object);
}

Get all value values in the dictionary
Nsenumerator * Enumeratorvalue = [dictionary objectenumerator];

Fast enumeration traversing values of all value
For (NSObject *object in Enumeratorvalue) {
NSLog (@ "Traverse value:%@", object);
}

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;
}

2. Create a variable Dictionary object

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

[Nsmutabledictionary Dictionarywithcapacity:10]: Create a variable dictionary to initially specify its length to be 10. Dynamic add data if more than 10 of this dictionary length will automatically increase, so do not worry about array bounds. Recommended in this way

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

[Dictionary setobject:@ "Rain Pine Momo" forkey:@ "name"]: Add data dynamically to a variable dictionary, where key is name and the value is Rain pine momo. If the key's data exists in the dictionary, the value of the key is replaced directly. (Easy to mix place, cautious.) )

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

[Dictionary Removeobjectforkey ...] : Deletes the data for 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 dynamically to the dictionary
[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;
}

The existence of dictionary class is to solve in a large number of data to find convenience, because it is through the key to find value directly so fast, avoid a traversal search caused by inefficient, the use of dictionary classes will help your program speed.

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


From: http://www.cnblogs.com/wengzilin/archive/2012/03/15/2397712.html

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.