Iphone 初始化NSMutableDictionary

Iphone 初始化NSMutableDictionary,iphone,nsdictionary,nsmutabledictionary,Iphone,Nsdictionary,Nsmutabledictionary,我正在尝试创建一个过滤器。我有一个函数,它为我可以拥有的不同类别返回一个NSArray。我想要一个额外的选项“All”来表示没有应用任何过滤器。例如,应显示: All Soda Wine Beer 我是从这个开始的,但我认为它根本不是很好的实现,而且它也不正确。这正是我目前所处的位置: - (void)initCategoryDictionary { NSMutableArray *conditionCategories = (NSMutableArray *)[self GetAll

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

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

- (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]; 
}
-(void)initCategoryDictionary{
NSMutableArray*条件类别=(NSMutableArray*)[self-GetAllCategories];
[conditionCategories insertObject:@“All”a索引:0];//第一个索引处没有筛选器大小写
NSMutableArray*对象值=[NSMutableArray];
[objectValues addObject:[NSNumber numberWithBool:YES]];//这是针对第一个索引处的“否”筛选器情况
对于(int i=0;i<[conditionCategories count];i++){
[objectValues addObject:[NSNumber numberWithBool:NO]];
}
self.CategoryDictionary=[NSMutableDictionary Dictionary WithObjects:objectValues forKeys:conditionCategories];
}
在方法的第一行,我不知道我是否应该打字。我的类别目前没有“全部”类别,因此这是我在将其插入词典之前将“全部”添加到NSArray的方法

我想我可以添加一个YES,NO,NO。。。。(字典中所有其他项目均为否)。这样,我的字典将默认为All为true(没有应用过滤器)。然后根据所选内容,我会将All更改为NO,将其他选定值更改为YES并进行适当过滤。(for循环是错误的,因为它只给我一个由2个对象组成的数组,值为0和1


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

如果
所有
都被应用了或没有应用,为什么不把它作为一个单独的布尔值,作为过滤器的一部分进行检查,而不用担心它在数组中呢?

可以做的一件事是将类别名称映射到
NSPredicate
的实例。每个谓词本身都会执行筛选(尽管我不清楚筛选的是什么)。因此,您可以按如下方式设置词典:

- (void)initCategoryDictionary {
    NSMutableArray *conditionCategories = (NSMutableArray *)[self GetAllCategories]; 
    [conditionCategories insertObject:@"All" atIndex:0];    // no filter case at first index
    NSMutableArray *objectValues = [NSMutableArray array];
    [objectValues addObject:[NSPredicate predicateWithFormat:@"TRUEPREDICATE"]; // this is for the no filter case at the first index
    for (NSString *category in conditionCategories) {
        [objectValues addObject:[NSPredicate predicateWithFormat:@"category == %@", category]];
    }
    self.CategoryDictionary = [NSMutableDictionary dictionaryWithObjects:objectValues forKeys:conditionCategories]; 
}

TRUEPREDICATE
将始终返回true,因此匹配任何内容。您必须编辑其他谓词以处理您要筛选的内容。

这在我看来非常困难。您尝试为字典初始化设置数组和值集,这是可能的,但对于您正在做的事情来说确实很困难我只需要制作一个NSMutableDictionary,为键添加值

-(void) initCategoryDictionary {
     NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithValue:[NSNumber numberWithBool:YES]] autorelease];

     NSArray *conditionCategories = [self GetAllCategories]; //No you dont need to type cast this as long as it returns the same type (NSArray here)

     for(NSString *str in conditionCategories) {
          [dict addValue:[NSNumber numberWithBool:NO] forKey:str];
     }

     [self setCategoryDictionary:dict];
}

这是个好主意。我想把所有内容都放在一个字典里会更容易,这样当我在CellForRowatineXpath方法中显示数据时,我就可以从该字典中提取数据。@Crystal——如果要把它塞进字典里,只需写很多乱七八糟的代码,那么在
cellForRowAtI中处理它可能也同样容易使用布尔测试的NDEPATH
方法。干杯!