NSDictionary
key --->value
索引 --->内容
存储键值对
(1)三种创建方式:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"Jony" forKey:@"name"];
NSArray *keys = @[@"name", @"location"]; NSArray *objects = @[@"Jony", @"China"]; NSDictionary *dict = [NSDictionary dictionaryWithObject:objects forKey:keys];
NSDictionary *dict = @{@"name" : @"Jony", @"location", @"China"};
(2)调用
id obj = [dict objectForKey:@"name"]; id obj = dict[@"name"];
NSMutableDictionary
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"Jony" forKey:@"name"]; //deleate [dict removeObjectForKey:(id)]; NSMutableDictionary *dict = @{@"name" : @"Jony", @"location", @"China"};
(1)字典遍历方式
NSArray *keys = [dict allKeys]; for (int i = 0; i < dict.count; i++) { NSString *key = keys[i]; NSString *object = dict[key]; NSLog(@"%@ = %@", key, object); }
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)] { NSLog(@"%@ -- %@", key, obj); //Only oneTime *stop = YES; }
时间: 2024-10-27 13:39:11