1.创建字典
// 1.如何创建
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lnj" forKey:@"name"];
NSString *name = [dict objectForKey:@"name"];
NSLog(@"name = %@", name);
// 注意: key和value 是一一对应
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lnj", @"30", @"1.75"] forKeys:@[@"name", @"age", @"height"]];
NSLog(@"%@ %@ %@", [dict objectForKey:@"name"], [dict objectForKey:@"age"], [dict objectForKey:@"height"]);
NSDictionary *dict = @{key:value};
NSDictionary *dict = @{@"name": @"lnj"};
NSLog(@"%@", dict[@"name"]);
NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"};
NSLog(@"%@ %@ %@", dict[@"name"], dict[@"age"], dict[@"height"]);
2.字典的遍历
(1)普通遍历
for (int i = 0; i < dict.count; ++i) {
获取字典中所有的key
NSArray *keys = [dict allKeys];
取出当前位置对应的key
NSLog(@"%@", keys[i]);
NSString *key = keys[i];
NSString *value = dict[key];
NSLog(@"key = %@, value = %@", key, value);
}
(2)快速遍历
// 如何通过forin遍历字典, 会将所有的key赋值给前面的obj
for (NSString *key in dict) {
NSLog(@"%@", key);
NSString *value = dict[key];
NSLog(@"key = %@, value = %@", key, value);
}
(3) 用迭代器遍历
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"key = %@, value = %@", key, obj);
}];
3.字典文件读写
// 3.字典文件读写
NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"};
// XML 扩展名plist
[dict writeToFile:@"/Users/xiaomage/Desktop/info.plist" atomically:YES];
// 注意: 字典和数组不同, 字典中保存的数据是无序的
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/xiaomage/Desktop/info.plist"];
NSLog(@"%@", newDict);