作者:岁月完好 | 来源:互联网 | 2023-10-11 14:21
今天来看一下Foundation框架中的NSDictionary类,NSMutableDictionary类,这个和Java中的Map类很想,OC中叫字典,Java中叫Map,还有
今天来看一下Foundation框架中的NSDictionary类,NSMutableDictionary类,这个和Java中的Map类很想,OC中叫字典,Java中叫Map,还有字典是无序的,这个和NSArray不一样,Java中的Map也是无序的,通过hash值去检索元素的。
一、NSDictionary类
-
- #import
-
- int main(int argc, const charchar * argv[]) {
- @autoreleasepool {
-
- NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"zhangfei", nil nil];
- NSArray *array2 = [NSArray arrayWithObjects:@"lisi",@"liping", nil nil];
-
-
-
- NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:array1,@"zhang",array2,@"li", nil nil];
- NSLog(@"%@",dict);
-
-
- NSDictionary *dict2 = [NSDictionary dictionaryWithObject:array1 forKey:@"zhang"];
-
-
-
- NSArray *allKeys = [dict allKeys];
-
-
-
-
- NSArray *allValues = [dict allValues];
-
-
-
- NSArray *values = [dict objectForKey:@"zhang"];
-
-
-
- NSDictionary *dict3 = @{@"zhangsan":array1,@"lisi":array2};
- NSLog(@"%@",dict3);
-
- NSArray *array4 = dict3[@"zhang"];
-
-
- }
- return 0;
- }
1、创建方法
- NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"zhangfei", nil nil];
- NSArray *array2 = [NSArray arrayWithObjects:@"lisi",@"liping", nil nil];
-
- 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
- NSArray *allKeys = [dict allKeys];
4、获取所有元素的value
- NSArray *allValues = [dict allValues];
5、通过key获取value
- NSArray *values = [dict objectForKey:@"zhang"];
6、NSDirctionary的快速创建方式和访问方式
- NSDictionary *dict3 = @{@"zhangsan":array1,@"lisi":array2};
- NSLog(@"%@",dict3);
-
- NSArray *array4 = dict3[@"zhang"];
二、NSMutableDictionary类
-
- #import
-
- int main(int argc, const charchar * argv[]) {
- @autoreleasepool {
-
- NSMutableDictionary *md1 = [[NSMutableDictionary alloc] initWithCapacity:3];
- NSArray *array1 = [[NSArray alloc] initWithObjects:@"zhangsan",@"lis", nil nil];
-
-
-
- [md1 setObject:array1 forKey:@"zhang"];
-
- NSMutableDictionary *md2 = [[NSMutableDictionary alloc] initWithCapacity:3];
- [md2 addEntriesFromDictionary:md1];
-
-
-
- [md1 removeObjectForKey:@"zhang"];
- [md1 removeAllObjects];
- [md1 removeObjectsForKeys:array1];
-
-
-
-
- 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.count;i++){
- NSArray *value = [md1 objectForKey:allKey[i]];
- }
-
-
-
-
-
- }
- return 0;
- }
之前的NSDictionary类是不可变字典,同样的OC中也有对应的可变字典:NSMutableDictionary
1、创建可变字典
- 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"];
- [md1 removeAllObjects];
- [md1 removeObjectsForKeys:array1];
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.count;i++){
- NSArray *value = [md1 objectForKey:allKey[i]];
- }
总结
这一篇文章就介绍了OC中的NSDirctionary类和NSMutableDirctionary类,他们的用途在后面非常关键,经常被用到。
OC学习篇之---Foundation框架中的NSDictionary类以及NSMutableDictionary类