[英]NSRangeException in loop
I'm still learning by doing, so please, if this sounds like a noob question, that's probably what it is. 我还在学习,所以请,如果这听起来像一个菜鸟问题,那可能就是它。
I'm trying to iterate through a NSDictionary (messeges) and grab the value on a certain key. 我正在尝试遍历NSDictionary(messeges)并获取某个键上的值。
When I run this code I get the following error" * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 3]'" 当我运行此代码时,我收到以下错误“ *由于未捕获的异常'终止应用'NSRangeException',原因:'* - [__ NSArrayI objectAtIndex:]:索引4超出边界[0 .. 3]'”
NSArray* keys = [messages allKeys];
int count = [keys count] ;
for (int i=0; i < count; i++) {
for(NSString* key in keys) {
if ([key isEqualToString:@"messagesinconversation"]) {
NSArray* arr = [messages objectForKey:key];
NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i];
NSLog (@"%@", sentby);
}
}
}
Could somebody point me in the right direction? 有人能指出我正确的方向吗?
I think the problem occured in 我认为问题出现了
NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i];May be the arr array count is lessthan keys array count.
NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i];code with below codeNSString * sentby = [[arr valueForKey:@“sentby”] objectAtIndex:i];
NSLog (@"%@", sentby);NSLog(@“%@”,sentby);
for(NSDictionary *dictionary in arr){for(NSrictionary * arr中的字典){
NSString *sentby=[dictionary objectForKey:@"sentby"];NSString * sentby = [字典objectForKey:@“sentby”];
NSLog(@"sentby is %@",sentby);NSLog(@“sentby is%@”,sentby);
}}
I guess problem is your running two loops 我想问题是你运行两个循环
Check this, 检查一下,
NSArray* keys = [messages allKeys];
int count = [keys count] ;
for (int i=0; i < count; i++) {
NSString *key=[keys objectAtIndex:i];
if ([key isEqualToString:@"messagesinconversation"])
{
NSArray* arr = [messages objectForKey:key];
NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i];
NSLog (@"%@", sentby);
}
}
Don't need to get value on objectAtIndex
in this array.So please see my edited code 不需要在这个数组中获得
objectAtIndex
值。所以请看我编辑的代码
NSArray* keys = [messages allKeys];
for(NSString* key in keys)
{
if ([key isEqualToString:@"messagesinconversation"])
{
NSArray* arr = [messages objectForKey:key];
NSString *sentby = [arr valueForKey:@"sentby"]; // here's your problem.
NSLog (@"%@", sentby);
}
}
It's unclear exactly what you want to do, but the root cause is the fact that i
is the current index into the keys
array, not the arr
array. 目前还不清楚你想要做什么,但根本原因是
i
是keys
数组的当前索引,而不是arr
数组。
Perhaps you want: 也许你想要:
NSArray *inconversation = [messages objectForKey:@"messagesinconversation"];
if (inconversation) {
NSArray *sentBy = [inconversation valueForKey:@"sentby"];
for (NSString *sender in sentBy) {
NSLog (@"%@", sender);
}
}
(why bother iterating through the dictionary keys when you want one specific element anyway?) (当你想要一个特定的元素时,为什么还要乱解字典键呢?)
The Problem is NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i]; 问题是NSString * sentby = [[arr valueForKey:@“sentby”] objectAtIndex:i];
i is greater than array count, check the i value and array count . 我大于数组计数,检查i值和数组计数。
better make a if condition when retrieving the value 在检索值时更好地创建if条件
if (i <= [array count]) like this if(i <= [array count])是这样的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.