2012-05-02 192 views
0

当我做了我的NSMutableArray的内容的NSLog的,它返回:ObjectAtIndex会导致应用程序崩溃

(
    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]); 
    }]; 
+2

这是不够的代码,看看什么是错的。你能提供更多的代码吗?例如,你创建/填充数组? – Manuel

+2

当你打印出你发布的数组内容时,你的'NSLog'调用了什么?而且,正如@ dragon112所说,发布更多代码,以便我们可以看到您的数组正在创建。 –

+0

@michaela在哪个类中做NSlog加上告诉我什么是应用程序ru的senerio使用任何sorta push pop,加上我能想到的事情是,你必须将数组设置为类的专业对象,(但这一切都取决于你的应用程序的senerio) – WaaleedKhan

回答

1

首次登录时它没有任何因方式完成工作的块的内容,试试这个:

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

希望这有助于!

山姆

0
always prefer to use this approach 

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

} 

计数器是否大于0无校验需要

Cheerz这将在循环进入:P