在里面找到字典键和字典数组

时间:2013-05-01 13:39:24

标签: ios nsmutablearray nsmutabledictionary

我有一个包含多个NSMutableDictionary个对象的数组。

每次填写文本字段并按下提交按钮:

//The strings
NSString *dateString = @"Date";
NSString *timeString = @"Time";
NSString *typeString = @"Type";

- (IBAction)addData:(id)sender {

[_myDictionary setObject:_dateLabel.text forKey:dateString];
[_myDictionary setObject:_nNumber.text forKey:timeString];
[_myDictionary setObject:_type.text forKey:typeString];

[_myArray addObject:_myDictionary];

[self display]
}

然后我试图在UItextView中显示结果(在此示例中为日期),但我不知道如何。

    -(void)displayData{

    for (int i = 0; i<_myArray.count; i++) {
        [_myArray objectAtIndex:i];
        outputString = [outputString stringByAppendingFormat:@"%@\n",[_myDictionary objectForKey:@"Date"]];

    }
    _myTextView.text = [NSString stringWithFormat:@"%@",outputString];

}

这最终显示数组中最后一个Dictionary的日期。我甚至无法弄清楚如何从特定字典中记录日期 说[_myArray objectAtIndex:3] [_myDictionary objectForKey:@"Date"]。我如何从第三个索引中记录日期?

希望这是有道理的

EDIT SOLUTION

for (int i = 0; i<_myArray.count; i++)
    {

        outputString = [outputString stringByAppendingFormat:@"%@ %@\n",[[_myArray objectAtIndex:i]objectForKey:@"Date"], [[_myArray objectAtIndex:i]objectForKey:@"Time"]];

    }
    _myTextView.text = [NSString stringWithFormat:@"%@",outputString];

}

感谢所有快速帮助!!

4 个答案:

答案 0 :(得分:6)

for(int i =0 ;i <[array count];i++)
{
  NSLog(@"%@",[[array objectAtIndex:i]allKeys]);
 }

此方法将打印所有键

答案 1 :(得分:1)

首先,每次向数组添加新对象时都必须分配一个新的字典,否则你的字典只会保留最后一个值,所以

 - (IBAction)addData:(id)sender 
 {

   NSMutableDictionary *_myDictionary=[[NSMutableDictionary alloc] init];

   [_myDictionary setObject:_dateLabel.text forKey:dateString];
   [_myDictionary setObject:_nNumber.text forKey:timeString];
   [_myDictionary setObject:_type.text forKey:typeString];

   [_myArray addObject:_myDictionary];

   [self display]
}

现在显示值的部分

-(void)displayData
{
   for (int i = 0; i<_myArray.count; i++) 
  {
      NSMutableDictionary *dictionary = [_myArray objectAtIndexPath:i];

      outputString = [outputString stringByAppendingFormat:@"%@\n",[dictionary objectForKey:@"Date"]];

   }
   _myTextView.text = [NSString stringWithFormat:@"%@",outputString]; 

}

答案 2 :(得分:0)

尝试

-(void)displayData{
    NSMutableString *outputString = [NSMutableString string];
    for (NSDictionary *dict in _myArray) {
        [outputString stringByAppendingFormat:@"%@\n",dict[@"Date"]];
    }

    _myTextView.text = [NSString stringWithString:outputString];
}

答案 3 :(得分:0)

您正在混合实例和局部变量,而不是在循环中正确更新。

理想情况下:

  1. myArray将是一个实例变量(就像你现在拥有的那样)。
  2. myDictionary将是您需要的每种方法的局部变量。
  3. 这可以区分持久数据和工作数据。

    请改为尝试:

    NSString *dateString = @"Date";
    NSString *timeString = @"Time";
    NSString *typeString = @"Type";
    
    - (IBAction)addData:(id)sender {
    
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
        [dict setObject:_dateLabel.text forKey:dateString];
        [dict setObject:_nNumber.text forKey:timeString];
        [dict setObject:_type.text forKey:typeString];
    
        [_myArray addObject:dict];
    
        [self display]
    }
    
    
    -(void)displayData{
    
        NSString *outputString = @"";
    
        for (int i = 0; i<_myArray.count; i++) {
            NSDictionary *dict = [_myArray objectAtIndex:i];
            outputString = [outputString stringByAppendingFormat:@"%@\n",[dict objectForKey:@"Date"]];
        }
    
        _myTextView.text = outputString;
    }