简体   繁体   English

ObjectAtIndex导致应用崩溃

[英]ObjectAtIndex causes app to crash

When I do an NSLog of the contents of my NSMutableArray, it returns : 当我对我的NSMutableArray的内容执行NSLog时,它返回:

(
    hat,
    hat
)

So why is it that when I do an NSLog like so NSLog(@"%@", [pro.matches objectAtIndex:0]); 那为什么当我像这样执行NSLog(@"%@", [pro.matches objectAtIndex:0]); it crashes with the error: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 它因错误而崩溃: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

So strange 这么奇怪

This is where I fill it: 这是我填写的地方:

[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(); 

and this is where i call it: 这就是我所说的:

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

When you first log the contents it has nothing in due to the way completion blocks work, try this: 当您第一次记录内容时,由于完成块的工作方式而没有任何内容,请尝试以下操作:

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

Hope this Helps! 希望这可以帮助!

Sam 山姆

always prefer to use this approach

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

}

this will enter in loop if the counter is greater than 0. no check needed 如果计数器大于0,它将进入循环。无需检查

Cheerz :P 切尔兹:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM