Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm trying to get object index from array using dictionary key object... but unable to get it.

Here are the details..

I have the Array having dictionaries....

 (
   {
        CATEGORYID = 865;
        CATEGORYNAME = "sample1";
        CATEGORYTHUMBIMAGEURL = "1.jpg";
    },
            {
        CATEGORYID = 140;
        CATEGORYNAME = "sample2";
        CATEGORYTHUMBIMAGEURL = "2.jpg";
    },
            {
        CATEGORYID = 1194;
        CATEGORYNAME = "sample3";
        CATEGORYTHUMBIMAGEURL = "3.jpg";
    }
); 

Here i have the CATEGORYID.... using category id i need to get the object index at array...

The code i have implemented was...

 int inx;
    for (int i=0; i<[mutArrData count]; i++) {
    NSString *str=(NSString*)[sender tag];
    //NSLog(@"MY Str %@",str);
    id myOjbect =(id)[sender tag];
    //NSLog(@"My OBJECT %d",[myOjbect intValue]);

    if ([[[mutArrData objectAtIndex:i] allKeys] containsObject:[NSString stringWithFormat:@"%d",[sender tag]]]) {
        inx=i;
        NSLog(@"Breaking here %d",inx);

    }

}

But unable to get it, can any one help...

share|improve this question
add comment

1 Answer

One side issue is that tag is an integer, and above you treat it as a NSString, an object and an integer.

Assuming you listed your array as some sample data, your main issue though is that allKeys is returning you an array of key names and not the values you want to compare.

What you want to do is pull out the int value for the categoryid key:

// Return matching index, or -1 for not found
-(int)indexInArray:(NSArray)array forCategoryId:(int)categoryId {
  for (int i = 0; i < [array count]; i++) {
    int currentCategoryId = [[[array objectAtIndex:i] valueForKey:@"CATEGORYID"] integerValue];
    if (currentCategoryId == categoryId) {
      return i;
    }
  }
  return -1;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.