OC learning --- NSDirctionary class and NSMutableDirctionary class in the Foundation framework, nsmutablearray

Source: Internet
Author: User
Tags allkeys

OC learning --- NSDirctionary class and NSMutableDirctionary class in the Foundation framework, nsmutablearray

I learned about NSArray and NSMutableArray classes in the Foundation framework yesterday.


I. NSDirctionary class

//// Main. m // 19_NSDictionary /// Created by jiangwei on 14-10-12. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> // a data set consisting of key-value pairs, int main (int argc, const char * argv []) {@ autoreleasepool {// 1. create NSArray * array1 = [NSArray arrayWithObjects: @ "zhangsan", @ "zhangfei", nil]; NSArray * array2 = [NSArray arrayWithObjects: @ "lisi ", @ "liping", nil]; // The first element: key: @ "zhang" value: array1 // The second element: key: @ "li" value: array2 NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: array1, @ "zhang", array2, @ "li", nil]; NSLog (@ "% @", dict ); // store an element NSDictionary * dict2 = [NSDictionary dictionaryWithObject: array1 forKey: @ "zhang"]; // 2. --------------------- obtain all key NSArray * allKeys = [dict allKeys]; // 3. --------------------- obtain all values // it may be a two-dimensional array NSArray * allValues = [dict allValues]; // 4. --------------------- obtain value NSArray * values = [dict objectForKey: @ "zhang"] through key; // 5. --------------------- optimized syntax NSDictionary * dict3 =@ {@ "zhangsan": array1, @ "lisi": array2}; NSLog (@ "% @", dict3 ); NSArray * array4 = dict3 [@ "zhang"];} return 0 ;}


1. Creation Method

// 1. create NSArray * array1 = [NSArray arrayWithObjects: @ "zhangsan", @ "zhangfei", nil]; NSArray * array2 = [NSArray arrayWithObjects: @ "lisi ", @ "liping", nil]; // The first element: key: @ "zhang" value: array1 // The second element: key: @ "li" value: array2NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: array1, @ "zhang", array2, @ "li", nil]; NSLog (@ "% @", dict );
We can see that the NSDirctionary class is created based on NSArray, and the rest is similar to the Map in Java, which is generally in the key-value format. Of course, here we also need to note that the end is: nil

Running result:



The printed result is a key-value style.


2. Add Elements

// Store an element NSDictionary * dict2 = [NSDictionary dictionaryWithObject: array1 forKey: @ "zhang"];

3. Get the key of all elements

// 2. --------------------- obtain all keyNSArray * allKeys = [dict allKeys];

4. Obtain the value of all elements

// 3. --------------------- obtain all values // it may be a two-dimensional array NSArray * allValues = [dict allValues];

5. Get value through key

// 4. --------------------- obtain valueNSArray * values = [dict objectForKey: @ "zhang"] through the key;

6. Fast creation and access modes of NSDirctionary

// 5. --------------------- optimized syntax NSDictionary * dict3 =@ {@ "zhangsan": array1, @ "lisi": array2}; NSLog (@ "% @", dict3 ); NSArray * array4 = dict3 [@ "zhang"];


Ii. NSMutableDirctionary class

//// Main. m // 20_NSMutableDictionary // Created by jiangwei on 14-10-12. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> // variable dictionary int main (int argc, const char * argv []) {@ autoreleasepool {// 1. ------------- create a variable dictionary with a size of 3 NSMutableDictionary * md1 = [[NSMutableDictionary alloc] centers: 3]; NSArray * array1 = [[NSArray alloc] centers: @ "zhangsan ", @ "lis", nil]; // 2. ------------- Add the element [md1 setObject: array1 forKey: @ "zhang"]; NSMutableDictionary * md2 = [[NSMutableDictionary alloc] initWithCapacity: 3]; [md2 priority: md1]; // 3. ------------- delete an element [md1 removeObjectForKey: @ "zhang"]; // Delete [md1 removeAllObjects] with a key; // delete all elements [md1 removeObjectsForKeys: array1]; // Delete with value // 4. ------------- looping dictionary // fast traversing for (NSString * key in md1) {NSArray * values = [md1 objectForKey: key]; for (NSString * v in values) {NSLog (@ "% @", v) ;}// normal traversal NSArray * allKey = [md1 allKeys]; for (int I = 0; I <allKey. count; I ++) {NSArray * value = [md1 objectForKey: allKey [I];} // The dictionary is unordered} return 0 ;}
The previous NSDirctionary class is an unchangeable dictionary, and the same OC also has the corresponding variable dictionary: NSMutableDirctionary

1. Create a variable dictionary

// 1. ------------- create a variable dictionary with a size of 3 NSMutableDictionary * md1 = [[NSMutableDictionary alloc] centers: 3]; NSArray * array1 = [[NSArray alloc] centers: @ "zhangsan ", @ "lis", nil];
Create a variable dictionary and specify its size. Of course, when the capacity is full, it automatically scales back.


2. Add Elements

// 2. ------------- Add the element [md1 setObject: array1 forKey: @ "zhang"]; NSMutableDictionary * md2 = [[NSMutableDictionary alloc] initWithCapacity: 3]; [md2 priority: md1];


3. Delete Elements

// 3. ------------- delete an element [md1 removeObjectForKey: @ "zhang"]; // Delete [md1 removeAllObjects] with a key; // delete all elements [md1 removeObjectsForKeys: array1]; // Delete the element with a value

4. traverse the dictionary

// 4. ------------- looping dictionary // fast traversing for (NSString * key in md1) {NSArray * values = [md1 objectForKey: key]; for (NSString * v in values) {NSLog (@ "% @", v) ;}// normal traversal NSArray * allKey = [md1 allKeys]; for (int I = 0; I <allKey. count; I ++) {NSArray * value = [md1 objectForKey: allKey [I];}


Summary

This article introduces the NSDirctionary and NSMutableDirctionary classes in OC. Their usage is very important and often used later.





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.