今天来看一下Foundation框架中的NSDictionary类,NSMutableDictionary类,这个和Java中的Map类很想,OC中叫字典,Java中叫Map,还有字典是无序的,这个和NSArray不一样,Java中的Map也是无序的,通过hash值去检索元素的。
一、NSDictionary类
- //
- // 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 charchar * argv[]) {
- @autoreleasepool {
- //1.---------------------字典的创建
- NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"zhangfei", nil nil];
- NSArray *array2 = [NSArray arrayWithObjects:@"lisi",@"liping", nil nil];
- //第一个元素:key:@"zhang" value:array1
- //第二个元素:key:@"li" value:array2
- NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:array1,@"zhang",array2,@"li", nil 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、创建方法
- //1.---------------------字典的创建
- NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"zhangfei", nil nil];
- NSArray *array2 = [NSArray arrayWithObjects:@"lisi",@"liping", nil nil];
- //第一个元素:key:@"zhang" value:array1
- //第二个元素:key:@"li" value:array2
- NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:array1,@"zhang",array2,@"li", nil nil];
- NSLog(@"%@",dict);
我们可以看到,NSDirctionary类的创建是基于NSArray的,然后其他的和Java中的Map就差不多了,一般是key-value的形式,当然这里也是需要注意结尾是:nil
运行结果:
打印结果是key-value的样式
2、添加元素
- //存放一个元素
- NSDictionary *dict2 = [NSDictionary dictionaryWithObject:array1 forKey:@"zhang"];
3、获取所有元素的key
- //2.---------------------获取所有的key
- NSArray *allKeys = [dict allKeys];
4、获取所有元素的value
- //3.---------------------获取所有的value
- //可能为二维数组
- NSArray *allValues = [dict allValues];
5、通过key获取value
- //4.---------------------通过key获取value
- NSArray *values = [dict objectForKey:@"zhang"];
6、NSDirctionary的快速创建方式和访问方式
- //5.---------------------优化的语法
- NSDictionary *dict3 = @{@"zhangsan":array1,@"lisi":array2};
- NSLog(@"%@",dict3);
- NSArray *array4 = dict3[@"zhang"];
二、NSMutableDictionary类
- //
- // 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 charchar * argv[]) {
- @autoreleasepool {
- //1.-------------创建一个可变字典,大小为3
- NSMutableDictionary *md1 = [[NSMutableDictionary alloc] initWithCapacity:3];
- NSArray *array1 = [[NSArray alloc] initWithObjects:@"zhangsan",@"lis", nil 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;
- }
之前的NSDictionary类是不可变字典,同样的OC中也有对应的可变字典:NSMutableDictionary
1、创建可变字典
- //1.-------------创建一个可变字典,大小为3
- NSMutableDictionary *md1 = [[NSMutableDictionary alloc] initWithCapacity:3];
- NSArray *array1 = [[NSArray alloc] initWithObjects:@"zhangsan",@"lis", nil nil];
创建一个可变字典,可以指定其大小,当然当容量满的之后,他回自动扩容
2、添加元素
- //2.-------------添加元素
- [md1 setObject:array1 forKey:@"zhang"];
- NSMutableDictionary *md2 = [[NSMutableDictionary alloc] initWithCapacity:3];
- [md2 addEntriesFromDictionary:md1];
3、删除元素
- //3.-------------删除元素
- [md1 removeObjectForKey:@"zhang"];//以key删除
- [md1 removeAllObjects];//删除所有元素
- [md1 removeObjectsForKeys:array1];//以value删除
4、遍历字典
- //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类,他们的用途在后面非常关键,经常被用到。