Thomas
Thomas

Reputation: 622

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.

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]'"

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?

Upvotes: 0

Views: 122

Answers (5)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Don't need to get value on objectAtIndex in this array.So please see my edited code

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

    }
 }

Upvotes: 0

MobileDev
MobileDev

Reputation: 1024

I think the problem occured in

NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i];
May be the arr array count is lessthan keys array count. But you are running the for loop based on the keys count. Replace
NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i];
NSLog (@"%@", sentby);
code with below code

for(NSDictionary *dictionary in arr){
   NSString *sentby=[dictionary objectForKey:@"sentby"];
   NSLog(@"sentby is %@",sentby);
}

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122458

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.

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?)

Upvotes: 0

srinivas n
srinivas n

Reputation: 640

The Problem is NSString *sentby = [[arr valueForKey:@"sentby"] objectAtIndex:i];

i is greater than array count, check the i value and array count .

better make a if condition when retrieving the value

if (i <= [array count]) like this

Upvotes: 0

Toseef Khilji
Toseef Khilji

Reputation: 17429

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

        }

}

Upvotes: 0

Related Questions