开发者

Initializing NSMutableDictionary

开发者 https://www.devze.com 2023-03-19 15:04 出处:网络
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 ex

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开发者_Python百科 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 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?


One thing you could do is map category names to instances of NSPredicate. Each predicate would itself perform the filter (although I'm not clear what's being filtered). So you could set up your dictionary as follows:

- (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 will always return true, and therefore match anything. You'll have to edit the other predicates to work on whatever it is you're filtering.


You're making this really hard IMO. You try to set up an array and values set for the dictionary initialization, which is possible, but really difficult for what you are doing. I would just make a NSMutableDictionary, add in the values for the keys.

-(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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消