初始化 NSMutableDictionary [英] Initializing NSMutableDictionary

查看:46
本文介绍了初始化 NSMutableDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建过滤器.我有一个函数,它为我可以拥有的不同类别返回一个 NSArray.我想要一个额外的全部"选项来表示没有应用过滤器.例如,应该显示:

I'm trying to create a filter. I have a function that returns an NSArray for the different categories I can have. I'd like one extra option for "All" to denote that no filter is being applied. For example, should display:

All
Soda
Wine
Beer

我是从这个开始的,但我认为它根本不是很好的实现,而且也不正确.这就是我目前所处的位置:

I started with this, but I don't think it's very good implementation at all, and it's not correct either. It's just where I'm at so far:

- (void)initCategoryDictionary {
    NSMutableArray *conditionCategories = (NSMutableArray *)[self GetAllCategories]; 
    [conditionCategories insertObject:@"All" atIndex:0];    // no filter case at first index
    NSMutableArray *objectValues = [NSMutableArray array];
    [objectValues addObject:[NSNumber numberWithBool:YES]]; // this is for the no filter case at the first index
    for (int i = 0; i < [conditionCategories count]; i++) {
        [objectValues addObject:[NSNumber numberWithBool:NO]];
    }
    self.CategoryDictionary = [NSMutableDictionary dictionaryWithObjects:objectValues forKeys:conditionCategories]; 
}

在方法的第一行,我不知道我是否应该对此进行类型转换.我的类别目前没有全部"类别,因此这是我可以在将其插入字典之前将全部"添加到 NSArray 的方法.

On the first line of the method, I don't know if I should be typecasting this or not. My categories currently does not have an "All" category, so this is the way I could figure out to add "All" to the NSArray before inserting it into the dictionary.

我想我可以添加一组 YES、NO、NO、....(对于字典中的所有其他项目,NO).这样,我的字典将默认为 All 为真(不应用过滤器).然后根据选择的内容,我将 All 更改为 NO,并将其他值选择为 YES 并适当过滤.(for 循环是错误的,因为它只给了我一个包含 0 和 1 作为我的值的 2 个对象的数组.

I thought I could then add an array of YES, NO, NO, .... (NO for all the other items in the dictionary). This way, my dictionary would default to All being true (no filter applied). Then based on what is selected, I would change the All to NO, and the other values selected to YES and filter appropriately. (The for loop is wrong since it only gives me an array of 2 objects with 0 and 1 as my values.

所以这就是我想我会做的,但显然这不是一个好的或正确的做事方式.有什么想法吗?谢谢!

So this is what I thought I would do, but obviously this isn't a good or correct way to do things. Any thoughts? Thanks!

推荐答案

如果 All 要么被应用,为什么不把它作为一个单独的布尔值作为过滤器的一部分来检查,并且不用担心它在数组中?

If All is either applied or not, why not just have it as a separate boolean value that you check as part of the filter, and don't worry about it being in the array itself?

这篇关于初始化 NSMutableDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆