昨天學習了Foundation框架中NSArray類和NSMutableArray類:http://blog.csdn.net/jiangwei0910410003/article/details/41809719,今天來看一下Foundation框架中的NSDirctionary類,NSMutableDirctionary類,這個和Java中的Map類很想,OC中叫字典,Java中叫Map,還有字典是無序的,這個和NSArray不同,Java中的Map也是無序的,經過hash值去檢索元素的。java
1、NSDirctionary類數組
// // main.m // 19_NSDictionary // // Created by jiangwei on 14-10-12. // Copyright (c) 2014年 jiangwei. All rights reserved. // #import <Foundation/Foundation.h> //由鍵值對key-value構成的數據集合 int main(int argc, const char * argv[]) { @autoreleasepool { //1.---------------------字典的建立 NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"zhangfei", nil]; NSArray *array2 = [NSArray arrayWithObjects:@"lisi",@"liping", nil]; //第一個元素:key:@"zhang" value:array1 //第二個元素:key:@"li" value:array2 NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:array1,@"zhang",array2,@"li", nil]; NSLog(@"%@",dict); //存放一個元素 NSDictionary *dict2 = [NSDictionary dictionaryWithObject:array1 forKey:@"zhang"]; //2.---------------------獲取全部的key NSArray *allKeys = [dict allKeys]; //3.---------------------獲取全部的value //可能爲二維數組 NSArray *allValues = [dict allValues]; //4.---------------------經過key獲取value NSArray *values = [dict objectForKey:@"zhang"]; //5.---------------------優化的語法 NSDictionary *dict3 = @{@"zhangsan":array1,@"lisi":array2}; NSLog(@"%@",dict3); NSArray *array4 = dict3[@"zhang"]; } return 0; }
//1.---------------------字典的建立 NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"zhangfei", nil]; NSArray *array2 = [NSArray arrayWithObjects:@"lisi",@"liping", nil]; //第一個元素:key:@"zhang" value:array1 //第二個元素:key:@"li" value:array2 NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:array1,@"zhang",array2,@"li", nil]; NSLog(@"%@",dict);咱們能夠看到,NSDirctionary類的建立是基於NSArray的,而後其餘的和Java中的Map就差很少了,通常是key-value的形式,固然這裏也是須要注意結尾是:nil
運行結果:框架
打印結果是key-value的樣式學習
二、添加元素優化
//存放一個元素 NSDictionary *dict2 = [NSDictionary dictionaryWithObject:array1 forKey:@"zhang"];
//2.---------------------獲取全部的key NSArray *allKeys = [dict allKeys];
//3.---------------------獲取全部的value //可能爲二維數組 NSArray *allValues = [dict allValues];
//4.---------------------經過key獲取value NSArray *values = [dict objectForKey:@"zhang"];
//5.---------------------優化的語法 NSDictionary *dict3 = @{@"zhangsan":array1,@"lisi":array2}; NSLog(@"%@",dict3); NSArray *array4 = dict3[@"zhang"];
2、NSMutableDirctionary類spa
// // main.m // 20_NSMutableDictionary // // Created by jiangwei on 14-10-12. // Copyright (c) 2014年 jiangwei. All rights reserved. // #import <Foundation/Foundation.h> //可變字典 int main(int argc, const char * argv[]) { @autoreleasepool { //1.-------------建立一個可變字典,大小爲3 NSMutableDictionary *md1 = [[NSMutableDictionary alloc] initWithCapacity:3]; NSArray *array1 = [[NSArray alloc] initWithObjects:@"zhangsan",@"lis", nil]; //2.-------------添加元素 [md1 setObject:array1 forKey:@"zhang"]; NSMutableDictionary *md2 = [[NSMutableDictionary alloc] initWithCapacity:3]; [md2 addEntriesFromDictionary:md1]; //3.-------------刪除元素 [md1 removeObjectForKey:@"zhang"];//以key刪除 [md1 removeAllObjects];//刪除全部元素 [md1 removeObjectsForKeys:array1];//以value刪除 //4.-------------循環遍歷字典 //快速遍歷 for (NSString *key in md1) { NSArray *values = [md1 objectForKey:key]; for(NSString *v in values){ NSLog(@"%@",v); } } //普通遍歷 NSArray *allKey = [md1 allKeys]; for(int i=0;i<allKey.count;i++){ NSArray *value = [md1 objectForKey:allKey[i]]; } //字典是無序的 } return 0; }以前的NSDirctionary類是不可變字典,一樣的OC中也有對應的可變字典:NSMutableDirctionary
一、建立可變字典.net
//1.-------------建立一個可變字典,大小爲3 NSMutableDictionary *md1 = [[NSMutableDictionary alloc] initWithCapacity:3]; NSArray *array1 = [[NSArray alloc] initWithObjects:@"zhangsan",@"lis", nil];建立一個可變字典,能夠指定其大小,固然當容量滿的以後,他回自動擴容
二、添加元素code
//2.-------------添加元素 [md1 setObject:array1 forKey:@"zhang"]; NSMutableDictionary *md2 = [[NSMutableDictionary alloc] initWithCapacity:3]; [md2 addEntriesFromDictionary:md1];
//3.-------------刪除元素 [md1 removeObjectForKey:@"zhang"];//以key刪除 [md1 removeAllObjects];//刪除全部元素 [md1 removeObjectsForKeys:array1];//以value刪除
//4.-------------循環遍歷字典 //快速遍歷 for (NSString *key in md1) { NSArray *values = [md1 objectForKey:key]; for(NSString *v in values){ NSLog(@"%@",v); } } //普通遍歷 NSArray *allKey = [md1 allKeys]; for(int i=0;i<allKey.count;i++){ NSArray *value = [md1 objectForKey:allKey[i]]; }
這一篇文章就介紹了OC中的NSDirctionary類和NSMutableDirctionary類,他們的用途在後面很是關鍵,常常被用到。
blog
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。ip