Objective-c----nsdictionary, nsmutabledictionary

Source: Internet
Author: User
Tags allkeys

Knowledge Points:

Definition: A dictionary (dictionary) is a collection of keywords and their definitions.

On the code:
/ * * nsdictionary Immutable Dictionary * */        ///1, common methods for creating dictionary objects        you need to assign key-value pairs when you have a Dictionary object in the bedstead, but in order: value, key (value in front, key in back form).         nsdictionary*dic1 = [[nsdictionaryAlloc] initwithobjectsandkeys:@"Wang", @"Name", @ -, @"Age", @"Man", @"Gender",Nil] ;NSLog( @"%@", Dic1);nsdictionary*dic2 = [nsdictionarydictionarywithobjectsandkeys:@"Zhen", @"Name", @ A, @"Age", @"NV", @"Gender",Nil] ;NSLog( @"%@", Dic2);Nsarray*keys = @[@"Name", @"Age", @"Gender"] ;Nsarray*values = @[@"Wang", @ -, @"Male"] ;//Create a Dictionary object when the number of two array elements must be the same        nsdictionary*DIC3 = [[nsdictionaryAlloc] Initwithobjects:values Forkeys:keys];NSLog( @"%@", DIC3);nsdictionary*DIC4 = [nsdictionaryDictionarywithobjects:values Forkeys:keys];NSLog( @"%@", DIC4);//Gets the number of key-value pairs in the dictionary by the Count method        NSLog( @"%ld", [Dic4 Count]);//Get all the keys in the dictionary        Nsarray*allkeys = [Dic4 AllKeys];NSLog( @"%@", AllKeys);//Get all the values in the dictionary        Nsarray*allvalues = [Dic4 allvalues];NSLog( @"%@", allvalues);//get its corresponding value in the dictionary by the specified key        IDObject = [Dic4 objectforkey:@"Age"] ;NSLog( @"%@", object); for(inti =0; i < [dic4 count]; i++) {IDkey = [AllKeys objectatindex:i];IDValue = [Dic4 Objectforkey:key];NSLog( @"%@", value);//[nsstring class] Returns an object of type NSString            //iskindofclass Determines whether value is an object of type NSString            NSString*result = [Value iskindofclass:[NSStringClass]]? @"YES": @"NO";NSLog( @"%@:%@-->%@", key, value, result); }//Dictionary of Syntactic sugar forms (note ': ' and ', ' are paired up)        nsdictionary*DIC5 = @{@"Name": @"Wang", @"Age": @ -, @"Gender": @"Man"} ;NSLog( @"%@", DIC5);/ * * nsmutabledictionary Variable dictionary * */        //initwithdictionary and Dictionarywithdictionary can change immutable dictionaries into mutable dictionaries        nsmutabledictionary*DIC6 = [[nsmutabledictionaryAlloc] INITWITHDICTIONARY:DIC5];NSLog( @"%@", DIC6);nsmutabledictionary*dic7 = [nsmutabledictionaryDICTIONARYWITHDICTIONARY:DIC5];NSLog( @"%@", DIC7);//Two types of initialization methods        nsmutabledictionary*dic8 = [[nsmutabledictionaryALLOC] init];NSLog( @"%@", DIC8);nsmutabledictionary*DIC9 = [nsmutabledictionaryDictionary];NSLog( @"%@", DIC9);//Increase key-value pairs[Dic9 setobject:@"Wang"forkey:@"Name"] ; [Dic9 setobject:@331forkey:@"Age"] ; [Dic9 setobject:@"Mam"forkey:@"Gender"] ;NSLog( @"%@", DIC9);//Modify the value of the existing key (note: If the key is already present, modify the corresponding value, add this key value pair if it does not exist)[Dic9 setobject:@ theforkey:@"Age"] ;NSLog( @"%@", DIC9);//To delete the corresponding key-value pair according to the specified key[Dic9 removeobjectforkey:@"Age"] ;NSLog( @"%@", DIC9);//Delete all key-value pairs[Dic9 removeallobjects];NSLog( @"%@", DIC9);

Objective-c----nsdictionary, nsmutabledictionary

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.