NSMutableDictionary不设置密钥值

时间:2014-08-03 14:04:37

标签: objective-c nsdictionary nsmutabledictionary

我有一个代码,其中现有的句柄日期在一个数组中,将月份的日期分开并将其放入一个NSMutableDictionary与它们各自的日期,该数组具有以下结构:

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

我使用此代码将此值放在NSMutableDictionary中:

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 个答案:

答案 0 :(得分:1)

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

试试这个:

```objc

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

} ```

答案 1 :(得分:1)

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 :(得分:0)

这是因为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/。它比手动解析日期更容易,也更灵活。