Objective-c dictionary (dictionary)

Source: Internet
Author: User

A dictionary is a collection of key-object data. Just like the definition of a word in a dictionary, you can use the object key to obtain the required value from the objective-c dictionary.

Keys in the dictionary must be single-valued. Although they are usually strings, they can also be any object type. The values associated with the key can be of any object type, but they cannot be nil.

 

The following is a dictionary class:

# Import <Foundation/nsdictionary. h> <br/> # import <Foundation/nsobject. h> <br/> # import <Foundation/nsstring. h> <br/> # import <Foundation/NSAID utoreleasepool. h> </P> <p> int main (INT argc, const char * argv []) <br/> {<br/> NSAID utoreleasepool * Pool = [NSAID utoreleasepool alloc] init]; </P> <p> // immutable dictionary <br/> nsdictionary * tires = [nsdictionary dictionarywithobjectsandkeys: <br/> @ "front-left", @ "T1 ", @ "front-Right", @ "T2", <br/> @ "back-left", @ "T3", @ "back-right", @ "T4 ", nil]; </P> <p> // display immutable dictionary <br/> nslog (@ "T1: % @", [Tires objectforkey: @ "T1"]); <br/> nslog (@ "T2: % @", [Tires objectforkey: @ "T2"]); <br/> nslog (@ "T3: % @", [TIRES objectforkey: @ "T3"]); <br/> nslog (@ "T4: % @", [Tires objectforkey: @ "T4"]); </P> <p> // mutable dictionary <br/> nsmutabledictionary * glossary = [nsmutabledictionary dictionary]; </P> <p> // store three entries in the glossary <br/> // use setobject: forkey: method to set key/value <br/> [Glossary setobject: @ "A class defined so other classes can inherit from it" <br/> forkey: @ "abstract class"]; <br/> [Glossary setobject: @ "to implment all the methods defined in a protocol" <br/> forkey: @ "adopt"]; <br/> [Glossary setobject: @ "storing an object for later use" <br/> forkey: @ "Archiving"]; </P> <p> // retrieve and display them <br/> // use objectforkey: Key Method to retrieve the value <br/> nslog (@ "abstract class: % @ ", [Glossary objectforkey: @" abstract class "]); <br/> nslog (@" adopt: % @ ", [Glossary objectforkey: @ "adopt"]); <br/> nslog (@ "Archiving: % @", [Glossary objectforkey: @ "Archiving"]); </P> <p> [pool drain]; <br/> return 0; </P> <p >}< br/>

 

 

Common NSDictionary methods:

 

+ (Id) dictionaryWithObjectsAndKeys: Create a dictionary using the key-object {key1, obj1}, {key2, obj2 }...

Obj1, key1, obj2, key2,..., nil;

 

-(Id) initWithObjectsAndKeys: Initialize the newly assigned dictionary as a key-object pair {key1, obj1} {key2, obj2}... create a dictionary

Obj1, key1, obj2, key2...., nil;

 

-(Unsigned int) count returns the number of records in the dictionary.

 

-(NSEnumerator *) keyEnumerator returns an NSEnumerator object for all keys in the dictionary.

 

-(NSArray *) keysSortedByVlaueUsingSelector: return the key array in the dictionary, which is sorted by the comparison method specified by selector.

(SEL) selector

 

-(ID) objectforkey: The key returns the object of the specified key.

 

 

Common NSMutableDictionary methods:

 

+ (ID) dictionarywithcapacity: size creates a variable dictionary using an initial specified size.

 

-(ID) initwithcapacity: Size initializes the new dictionary to the specified size

 

-(Void) removeAllObjects delete all records in the dictionary

 

-(Void) removeObjectForKey: The key deletes the record corresponding to the specified key in the lyrics.

 

-(Void) setObject: obj forKey: key: Add obj to the dictionary as the key. If the key already exists, replace this value.

 

 

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.