代码之家  ›  专栏  ›  技术社区  ›  user3781174

NSMutableDictionary未设置键的值

  •  0
  • user3781174  · 技术社区  · 10 年前

    我有一个代码,其中数组中的现有句柄日期分隔了一个月的日期,并将其放入一个NSMutabbleDictionary中,其中包含各自的月份日期,该数组具有以下结构:

    "12/01/2014" //Structure of my date is -> dd-mm-yyyy
    "16/01/2014"
    "30/01/2014"
    "02/02/2014"
    "08/02/2014"
    

    我使用以下代码将这些值放入NSMutabbleDictionary中:

    dictionary = [NSMutableDictionary dictionary];
    for(int x=0;x<[list count];x++){
    
            NSMutableArray *lstaInfo2 = [[list[x] componentsSeparatedByString:@"/"] mutableCopy];
    
            if([lstaInfo2[1]  isEqual: @"01"]){
    
                [dictionary setValue:list[x] forKey:[NSString stringWithFormat:@"January of %@",lstaInfo2[2]]];
    
            }
            if([lstaInfo2[1]  isEqual: @"02"]){
    
                [dictionary setValue:list[x] forKey:[NSString stringWithFormat:@"February of %@",lstaInfo2[2]]];
            }
    
    }
    

    我希望在变量字典中返回的值是:

    January of 2014 =>
    
    "12/01/2014"
    "16/01/2014"
    "30/01/2014"
    
    February of 2014 =>
    
    "02/02/2014"
    "08/02/2014"
    

    但是变量字典只返回最后的值,如下所示:

    January of 2014 =>
    
        "30/01/2014"
    
    February of 2014 =>
    
        "08/02/2014"
    

    为什么?我如何解决这个问题?

    3 回复  |  直到 10 年前
        1
  •  1
  •   Hot Licks    10 年前
    dictionary = [NSMutableDictionary dictionary];
    for(int x=0;x<[list count];x++){
    
        NSString* key = [self getKeyForDate:list[x]];
        NSMutableArray* listForMonth = dictionary[key];
        if (key == nil) {
            listForMonth = [NSMutableArray array];
            [dictionary setValue:listForMonth forKey:key];
        }
        [listForMonth addObject:list[x]];
    }
    
    .....
    

    在里面 init

    monthArray = @[@"January", @"February", @"March" ...
    

    单独方法:

    -(NSString*) getKeyForDate:(NSString*)date {
        NSMutableArray *lstaInfo2 = [date componentsSeparatedByString:@"/"] mutableCopy];
        NSInteger monthNum = lstaInfo2[0].integerValue;
        NSString* result = [NSString stringWithFormat;@"%@ of %@", monthArray[monthNum-1], lstaInfo2[2]]; 
        return result;
    }
    

    您还可以使用NSDateFormatter来解析日期,使用NSCalendar来提供月份名称,但这对于一节课来说太深奥了。

        2
  •  1
  •   Rafał Augustyniak    10 年前

    当您尝试向字典中添加新值时,请检查该键的值是否实际在字典中。如果没有,创建并设置 NSMutableArray 对象,并将值添加到此数组。

    试试看:

    ```目标

    dictionary = [NSMutableDictionary dictionary];
    for(int x=0; x < [list count]; x++){
    
        NSArray *lstaInfo2 = [list[x] componentsSeparatedByString:@"/"];
        NSString *key;
    
        if([lstaInfo2[1]  isEqual: @"01"]){
            key = [NSString stringWithFormat:@"January of %@",lstaInfo2[2]];
    
        }
        if([lstaInfo2[1]  isEqual: @"02"]){
            key = [NSString stringWithFormat:@"February of   %@",lstaInfo2[2]];
        }
        if (key) { 
            //if there is no value for the specified key create and set 
            //NSMutableArray object for this key, otherwise keep value for
            //the key without modyfing it.
            dictionary[key] = dictionary[key] ?: [NSMutableArray array]; 
            [dictionary[key] addObject:list[x]];
        }
    

    } ```

        3
  •  0
  •   andrewlamb    10 年前

    这是因为 setValue:forKey: 不会追加到当前值。所以在循环的第一次迭代中 "January of 2014" => ["12", "01", "2014"] 在下一次迭代中 "January of 2014" => ["16", "01", "2014"] 等等。您希望从字符串映射到NSMutableArray。

    我真的建议您为此使用NSDateFormatter类: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/nsdateformatter_class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/ 。它将比手动解析日期更容易、更灵活。