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];
}
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.
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!
If
Allis 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?