OC Learning Notes Foundation Collection Class Dictionary dictionary

Source: Internet
Author: User

A dictionary of data sets consisting of key-value pairs

4 Ways to create dictionaries

1> direct assignment for a single key-value pair

 1Nsdictionary *dic = [Nsdictionary dictionarywithobject:@"Dajie"Forkey:@"name"];//Create a dictionary directly 2     ID Object= [dic Objectforkey:@"name"];//remove the name key corresponding value 3NSLog (@"%@",Object);

2> creation of a collection with key values

1Nsarray *obj = @[@"Dajie",@"ly"];2Nsarray *keys = @[@"name",@"Address"];3Nsdictionary *dic1 =[Nsdictionary dictionarywithobjects:obj Forkeys:keys]; //Create a dictionary 4NSLog (@"%@", Dic1);

3> Create and assign values directly

1      Nsdictionary *dic2 = [nsdictionary dictionarywithobjectsandkeys:@ "Dajie",@ " name ",@" ly"@"address", Nil];//nil not less  2      NSLog (@ "\ n---%@", Dic2);
3      id obj2 = [dic2 objectforkey:@ "name"];//based on key to remove corresponding value 4      NSLog (@ "%@", obj2); 

Commonly used in 4> development

//Xcode is commonly used in the development of new features theNsdictionary *dic3 = @{@"name":@"Dajie",@"Address":@"ly",@"QQ":@"12324124"}; -NSLog (@"%@\n--%@--%ld", DIC3,[DIC3 Objectforkey:@"name"],dic3.count);

Variable dictionaries

2      Nsmutabledictionary *mdic = [Nsmutabledictionary dictionary];//created

Adding elements and deleting elements

//to add a key-value pair -[MDic setobject:@"Dajie"Forkey:@" Person"]; +[MDic setobject:@"Hashiqi"Forkey:@"Dog"]; A[MDic setobject:@"Shapi"Forkey:@"Dog"];//The primary key is the same as the dictionary element, which is overwritten atNSString *str = mdic[@"Dog"]; -NSLog (@"%@", str); -   //Delete -[MDic removeallobjects]; -[MDic Removeobjectforkey:@"Dog"]; -NSLog (@"%@", MDic);

Traversing elements

1 Basic for Loop traversal

Nsdictionary *newdic = @{ to                              @"name":@"Dajie", +                              @"Dog":@"Hashiqi"
}; -Nsarray *key = [Newdic AllKeys];//Take out all the keys the for(intI i < Newdic.count; i++) { *NSString *keys =Key[i]; $NSString *value =Newdic[keys];Panax NotoginsengNSLog (@"%@ = %@", keys,value); -}

2 Simple traversal

// It is recommended to use fast traversal  +     enumeratekeysandobjectsusingblock: ^ (ID-IDBOOL *stop) {         NSLog (@ "", key, obj);      ];

OC Learning Notes Foundation Collection Class Dictionary dictionary

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.