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

目标C程序设计中单键多值NSmutabledictionary

  •  9
  • suse  · 技术社区  · 15 年前

    请告诉我,在nsmutabledictionary中,同一个键如何具有多个值?

    当我使用下面的方法时,这些值将替换为最近的值。

    在我看来:

    [dictionary setObject:forename forKey:[NSNumber numberWithint:code]];
    [dictionary setObject:surname forKey:[NSNumber numberWithint:code]];
    [dictionary setObject:reminderDate forKey:[NSNumber numberWithint:code]];
    

    当我查看字典的内容时,我只得到 reminderDate 用于关键代码。这里,所有值的代码都相同。我怎样才能避免名字和姓氏被替换 plannedReminder .

    谢谢您!

    4 回复  |  直到 8 年前
        1
  •  15
  •   dreamlax    15 年前

    你好像在用 code 作为键,您希望基于 代码 . 在这种情况下,您应该:

    1. 提取与关联的所有数据 代码 分成一个单独的类(可能称为 Person )并将此类的实例用作字典中的值。

    2. 使用多个词典层:

      NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
      
      NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
      [firstOne setObject:forename forKey:@"forename"];
      [firstOne setObject:surname forKey:@"surname"];
      [firstOne setObject:reminderDate forKey:@"reminderDate"];
      
      [dictionary setObject:firstOne forKey:[NSNumber numberWithInt:code]];
      
      // repeat for each entry.
      
        2
  •  5
  •   Hobey Kuhn    14 年前

    如果你真的坚持要把对象存储在字典中,如果你处理的是字符串,你可以把所有的字符串用逗号隔开,然后当你从键中检索对象时,你的所有对象都将是准csv格式的!然后可以轻松地将该字符串解析为对象数组。

    下面是一些可以运行的示例代码:

    NSString *forename = @"forename";
    NSString *surname = @"surname";
    NSString *reminderDate = @"10/11/2012";
    NSString *code = @"code";
    
    NSString *dummy = [[NSString alloc] init];
    dummy = [dummy stringByAppendingString:forename];
    dummy = [dummy stringByAppendingString:@","];
    dummy = [dummy stringByAppendingString:surname];
    dummy = [dummy stringByAppendingString:@","];
    dummy = [dummy stringByAppendingString:reminderDate];
    dummy = [dummy stringByAppendingString:@","];
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:dummy forKey:code];
    

    然后检索并分析字典中的对象:

    NSString *fromDictionary = [dictionary objectForKey:code];
    NSArray *objectArray = [fromDictionary componentsSeparatedByString:@","];
    NSLog(@"object array: %@",objectArray);
    

    它可能不像Dreamlax建议的那样干净,但是如果你正在处理一个字典,你想为一个键存储一个数组,而该数组中的对象本身没有特定的键,这是一个解决方案!

        3
  •  2
  •   Paul Schreiber Jon Lin    15 年前

    我想你不明白字典是怎么工作的。每个键只能有一个值。您将需要一个字典或数组字典。

    在这里,您为每个人创建一个词典,然后将其存储在主词典中。

    NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
    forename, @"forename", surname, @"surname", @reminderDate, "@reminderDate", nil];
    
    [dictionary setObject:d forKey:[NSNumber numberWithint:code]];
    
        4
  •  1
  •   Andres Canella    11 年前

    现代语法要干净得多。

    A.如果在加载时构建静态结构:

    NSDictionary* dic = @{code : @{@"forename" : forename, @"surname" : surnamem, @"reminderDate" : reminderDate}/*, ..more items..*/};
    

    B.如果您实时(可能)添加项目:

    NSMutableDictionary* mDic = [[NSMutableDictionary alloc] init];
    [mDic setObject:@{@"forename" : forename, @"surname" : surnamem, @"reminderDate" : reminderDate} forKey:code];
    //..repeat
    

    然后你进入一个二维字典…

    mDic[code][@"forename"];