0

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?

2
  • Should be an issue with your for statement looping....[for (int i=0; i < count; i++)] NSlog the 'count variable' and 'i' variable Commented Sep 12, 2013 at 10:50
  • 1
    Why [[arr valueForKey:@"sentby"] objectAtIndex:i]; as i has no relation to the arr array, it's an index into the keys array. Commented Sep 12, 2013 at 10:52

5 Answers 5

2

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why not NSArray *sentBy = [arr valueForKey:@"sentby"];?
I finally had a chance to test your code. This did it for me. Thx a lot.
0

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

        }

}

Comments

0

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

Comments

0

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

Comments

0

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

    }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.