首页 > 代码库 > 可变字典
可变字典
//1.创建一个可变长度字典 NSMutableDictionary * muDic = [[NSMutableDictionary alloc] initWithCapacity:0]; //2.向字典中村如数据 [muDic setObject:@"1" forKey:@"one"]; [muDic setObject:@"2" forKey:@"two"]; [muDic setObject:@"3" forKey:@"three"]; NSLog(@"muDic %@",muDic); //3.删除 [muDic removeObjectForKey:@"one"]; NSLog(@"muDic %@",muDic); //4.全部删除 [muDic removeAllObjects]; NSLog(@"muDic %@",muDic); Student * stu1 = [[Student alloc] initWithName:@"xiaoher" andAge:20]; Student * stu2 = [[Student alloc] initWithName:@"alex" andAge:50]; Student * stu3 = [[Student alloc] initWithName:@"xiaoli" andAge:10]; [muDic setObject:stu1 forKey:@"s1"]; [muDic setObject:stu2 forKey:@"s2"]; [muDic setObject:stu3 forKey:@"s3"]; //在向字典中存储数据的时候,一定要保证key值是唯一的// [muDic setObject:stu3 forKey:@"s3"];// [muDic setObject:stu3 forKey:@"s3"];// [muDic setObject:stu3 forKey:@"s3"]; NSLog(@"muDic %@",muDic); //1.使用for循环遍历字典 NSArray * allkeys = [muDic allKeys]; for (int i = 0; i < allkeys.count; i++) { NSString * key = [allkeys objectAtIndex:i]; Student * stu = [muDic objectForKey:key]; NSLog(@"stu %@",stu); }; //2.使用枚举器 NSEnumerator * enumerator = [muDic objectEnumerator]; Student * tmp; while (tmp = [enumerator nextObject]) { NSLog(@"tmp %@",tmp); }
可变字典
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。