3

コードで実際に発生しているエラーを特定するのに苦労しています。それは言います:

reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

アプリがクラッシュする部分です。

NSString *keyTemp = [[NSString alloc] initWithFormat:@"Wit%d",indexPath.section+1];
    NSArray *arrTemp;
    if ([userStandards objectForKey:keyTemp] == nil || [[userStandards objectForKey:keyTemp] count]==3) {
        if ([witDict objectForKey:keyTemp] == nil) {
            arrTemp = [[NSArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",nil];
        } else {
            arrTemp = [[NSArray alloc] initWithArray:[witDict objectForKey:keyTemp]];
        }
        cell.inputTextArea.text = [NSString stringWithFormat:@"Name: %@\nPhone: %@\nEmail: %@\nCity/State: %@\nZip: %@\nComments: %@",[arrTemp objectAtIndex:0],[arrTemp objectAtIndex:1],[arrTemp objectAtIndex:2], [arrTemp objectAtIndex:3], [arrTemp objectAtIndex:4], [arrTemp objectAtIndex:5]];
    } else {
        arrTemp = [[NSArray alloc] initWithArray:[userStandards objectForKey:keyTemp]];
        cell.inputTextArea.text = [NSString stringWithFormat:@"Name: %@\nPhone: %@\nEmail: %@\nCity/State: %@\nZip: %@\nComments: %@",[arrTemp objectAtIndex:0],[arrTemp objectAtIndex:1],[arrTemp objectAtIndex:2], [arrTemp objectAtIndex:3], [arrTemp objectAtIndex:4], [arrTemp objectAtIndex:5]];
}
4

2 に答える 2

2

コードを確認してください:

if ([[userStandards objectForKey:keyTemp] count]==3)
{

  // Your array only have 3 elements (index 0,1, and 2)

  // Here you accessing index beyond 2
  cell.inputTextArea.text = [NSString stringWithFormat:@"Name: %@\nPhone: %@\nEmail: %@\nCity/State: %@\nZip: %@\nComments: %@",[arrTemp objectAtIndex:0],[arrTemp objectAtIndex:1],[arrTemp objectAtIndex:2], [arrTemp objectAtIndex:3], [arrTemp objectAtIndex:4], [arrTemp objectAtIndex:5]];
}
于 2012-12-19T06:54:01.207 に答える
0

次のように使用します。

次の行は正しい 6 つの値が arrTemp にあります。

arrTemp = [[NSArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",nil];

しかし、この 3 つだけが arrTemp に入っています。

arrTemp = [[NSArray alloc] initWithArray:[witDict objectForKey:keyTemp]];

2 つ目に入力しています....keyTemp にキー/値を追加するか、ロジックを変更します。

于 2012-12-19T07:00:09.073 に答える