不能放非OC对象,不能放nil,key不能重复(重复不会报错)
写法是:
@{@"key":@"value"};
NSDictionary*person_dict=@{@"name":@"zhangsan",@"age":@22,@"height":@123,@"name":@"lisi"};
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#if 0
NSDictionary *person_dict =@{@"name":@"zhangsan",@"age":@22,@"height":@123,@"name":@"lisi"};
NSLog(@"%@",person_dict[@"name"]);
NSLog(@"count->%ld",person_dict.count);
NSLog(@"allKeys = %@",[person_dict allKeys]);
NSLog(@"allvalue = %@",[person_dict allValues]);
for (NSString *key in [person_dict allKeys]) {
NSLog(@"%@:%@",key,person_dict[key]);
}
[person_dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnullobj, BOOL * _Nonnull stop) {
NSLog(@"%@:%@",key,obj);
}];
// person_dict[@"name"] = @"wangwu";
#endif
#if 0
NSDictionary *dict =@{@"name":@"zhangsan",@"age":@123,@"height":@123.4,@"weight":@223,@"id":@"1122"};
Person *person = [[Person alloc]initWithDict:dict];
NSLog(@"name = %@",person.name);
NSLog(@"weight:%f",person.weight);
// NSLog(@"id = %@",person._id);
#endif
//#if 0
//可变字典
NSMutableDictionary *mutiDict = @{}.mutableCopy;
mutiDict = [NSMutableDictionary dictionary];
mutiDict[@"name"] = @"zhangsan";
mutiDict[@"age"] = @111;
mutiDict[@"height"] = @222;
//改名字
NSLog(@"mutiDict = %@",mutiDict);
mutiDict[@"name"] = @"zhangsi";
NSLog(@"mutiDict = %@",mutiDict);
//删除名字
[mutiDict removeObjectForKey:@"name"];
NSLog(@"mutiDict = %@",mutiDict);
NSMutableDictionary *scoreDict = [NSMutableDictionary dictionary];
NSArray *scoreItems = @[@22,@66,@33,@89.5,@99.8,@66.7];
//查找需要的数据,先用枚举遍历数组scoreItems
[scoreItems enumerateObjectsUsingBlock:^(NSNumber *score, NSUInteger idx, BOOL *_Nonnull stop) {
//随便赋个值,用来标记这个score,方便下面查找。
scoreDict[score] = @1;
}];
//判断是否有89.5
if(scoreDict[@89.5]){
NSLog(@"have");
}else{
NSLog(@"haven't");
}
@end