<pre name="code" class="objc"><span style="font-size:14px;">#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
字典 NSDictionary / NSMutableDictionary
1.概念:字典是关键字 'key' 及其定义的值 'value' 所构成的集合,即它是由key-value组成的键值对的集合。通常来讲,'key'一般是'NSString'类型的,'value'是OC中任意对象。对于字典来讲,我们就是通过'key'来访问'value'。
2.为什么在有了数组以后还需要字典呢?在由大量的数据或者频繁查找信息,使用字典效率更高。因为字典是采用键查询的优化存储方法的。
3.在OC中,我们使用 NSDictionary 和 NSMutableDictionary 来表示字典。其中 NSMutableDictionary 是 NSDictionary 的子类
*/
//初始化
//创建一个空的字典
NSDictionary *dictionary1 = [[NSDictionary alloc]init];
NSDictionary *dictionary2 = [NSDictionary dictionary];
//创建一个键值对的字典
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"Clown" forKey:@"name"];
//创建多个键值对的字典
NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjectsAndKeys:@"Black", @"name", @21, @"age", @"GZ", @"address", nil];
//字典里面存放的元素都要是对象类型才可以。如果是基本数据类型要先把这些基本数据类型进行封装,然后再放到这些集合里。
// value 在前, key 在后,他们要成双出现
// NSLog(@"%@",dictionary4);
//在字典中,键值是无序的
//根据一个字典创建一个字典
NSDictionary *dictionary5 = [NSDictionary dictionaryWithDictionary:dictionary4];
// NSLog(@"%@",dictionary5);
//新语法 创建字典
NSDictionary *dictionary6 = @{};
//新语法 创建一个键值对
NSDictionary *dictionary7 = @{@"name":@"Clown"};
// NSLog(@"%@",dictionary7);
//新语法 创建多个键值对
NSDictionary *dictionary8 = @{@"name":@"Clown",@"age":@21,@"address":@"GZ"};
// NSLog(@"%@",dictionary8);
//从本地读取字典
NSDictionary *dictionary9 = [NSDictionary dictionaryWithContentsOfFile:@""];
//从网络读取字典
NSDictionary *dictionary10 = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@""]];
//字典中键值对的数量
NSUInteger count = dictionary4.count;
// NSLog(@"count = %lu",count);
//如何访问字典
//通过key访问字典中的对应值
NSString *name = [dictionary4 objectForKey:@"name"];
// NSLog(@"%@",name);
//等价于
//新语法
NSString *newName = dictionary4[@"name"];
// NSLog(@"%@",newName);
//如何遍历字典中所有的值
//表示字典中所有的key
NSArray *keysArray = dictionary4.allKeys;
// NSLog(@"%@",keysArray);
//字典中所有的value
NSArray *valuesArray = dictionary4.allValues;
// NSLog(@"%@",valuesArray);
//使用for循环来打印
for (int i = 0; i < dictionary4.count; i++) {
// NSLog(@"%@ = %@",keysArray[i],valuesArray[i]);
// NSLog(@"%@ = %@",keysArray[i],[dictionary4 objectForKey:keysArray[i]]);
// NSLog(@"%@ = %@",keysArray[i],dictionary4[keysArray[i]]);
}
//使用快速枚举 forin
for (NSString *key in keysArray) {
// NSLog(@"%@ = %@", key, [dictionary4 objectForKey:key]);
// NSLog(@"%@ = %@", key, dictionary4[key]);
}
//使用枚举器遍历
NSEnumerator *enumerator = [keysArray objectEnumerator];
NSString *key;
while (key = [enumerator nextObject]) {
// NSLog(@"%@ = %@", key, dictionary4[key]);
}
//在不可变字典中不能存储相同的key,如果相同的key,则只会打印一个key对应的value
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Clown", @"name", @21, @"age", @"GZ", @"address", @"Black", @"name", nil];
for (NSString *key in [dictionary allKeys]) {
NSLog(@"%@ = %@", key, dictionary[key]);
}
//字典的输出是无序的
}
return 0;
}
</span>
下章讲
NSMutableDictionary (可变字典)
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="font-size: 14px;">其实 </span></span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;">NSMutableDictionary (可变字典) 和 </span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;">NSMutableArray(可变数组) 的用法相似</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;">
</span><span style="font-family:Arial, Helvetica, sans-serif;"><span style="font-size: 14px;">
</span></span><span style="font-size:14px;">
</span>