0

NSMutableArray の内容の NSLog を実行すると、以下が返されます。

(
    hat,
    hat
)

では、NSLog を次のように実行するNSLog(@"%@", [pro.matches objectAtIndex:0]);と、エラーでクラッシュするのはなぜですか。*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

とても奇妙

これは私がそれを埋める場所です:

[self.matches removeAllObjects];

         NSArray *JSONarray = [[NSArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]];
         int i;

         for (i=0; i<[JSONarray count]; i++) {
             //[self.matches addObject:[JSONarray objectAtIndex:i]];
             [self.matches addObject:@"hat"];
         }
         //NSLog(@"boys with bo%@", [[matches objectAtIndex:1] objectForKey:@"host"]);
         block();
         //JSON and add to matches.
     }
     }];
    block(); 

そして、これは私がそれを呼び出す場所です:

[pro refreshMatchesWithCallback:^ 
    {
        //[self.tableView reloadData];
        NSLog(@"the count of a lifetime is %@", [pro.matches objectAtIndex:0]);
    }];
4

2 に答える 2

1

完了ブロックが機能する方法のために何もないコンテンツを最初にログに記録するときは、次のことを試してください。

[pro refreshMatchesWithCallback:^ 
{
    //[self.tableView reloadData];
    if(pro.matches.count > 0) {
       NSLog(@"the count of a lifetime is %@", [pro.matches objectAtIndex:0]);
    }
}];

お役に立てれば!

サム

于 2012-05-02T14:52:22.737 に答える
0
always prefer to use this approach

for(Object* obj in arra)
{
...

}

カウンターが 0 より大きい場合、これはループに入ります。チェックは不要です。

Cheerz :P

于 2012-05-02T14:56:44.137 に答える