0

I want a looping concept in order to find whether an id in my code is matched or same as that of already present in the array.(for example I want to check whether the id 12345 is present in the following NSMutableArray) and also wants to know on which index path it has occurred and how to change that

{
    artist = "Green Day";
    id = 1421768;
    name = "American Idiot";
    releasedate = "21 Sep 2004";
    runningtime = "57.53";
    tracks = "1: American Idiot\n2: Jesus of Suburbia\n3: Holiday/Boulevard Of Broken Dreams\n4: Are We The Waiting/St. Jimmy\n5: Give Me Novacaine/She's A Rebel\n6: Extraordinary Girl/Letterbomb\n7: Wake Me Up When September Ends\n8: Homecoming\n9: Whatsername\n";
    trackscount = 9;
    type = 1;
},
    {
    artist = Bastille;
    id = 309124896;
    name = "Bad Blood";
    releasedate = "1 Mar 2013";
    runningtime = "43.98";
    tracks = "1: Pompeii\n2: Things We Lost in the Fire\n3: Bad Blood\n4: Overjoyed\n5: These Streets\n6: Weight of Living, Pt. II\n7: Icarus\n8: Oblivion\n9: Flaws\n10: Daniel in the Den\n11: Laura Palmer\n12: Get Home\n13: Weight of Living, Pt. I\n";
    trackscount = 13;
    type = 1;
},
    {
    artist = "Lacuna Coil";
    id = 2025689;
    name = Comalies;
    releasedate = "16 Oct 2012";
    runningtime = "51.75";
    tracks = "1: Swamped\n2: Heaven's a Lie\n3: Daylight Dancer\n4: Humane\n5: Self Deception\n6: Aeon\n7: Tight Rope\n8: The Ghost Woman and the Hunter\n9: Unspoken\n10: Entwined\n11: The Prophet Said\n12: Angels Punishment\n13: Comalies\n";
    trackscount = 13;
    type = 1;
}
Roby
  • 3
  • 5
  • you can use -> [NSMutableArray containsObject : YourID]; – Zღk May 29 '17 at 10:11
  • 1
    Possible duplicate of [Filtering NSArray of NSDictionary objects using NSPredicate](https://stackoverflow.com/questions/10505154/filtering-nsarray-of-nsdictionary-objects-using-nspredicate) – Vishnu gondlekar May 29 '17 at 10:13
  • `indexOfObjectPassingTest:` should do the trick. – Larme May 29 '17 at 12:22
  • Considering that "id" value is a NSString object: `NSUInteger index = [array indexOfObjectPassingTest:^BOOL(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { return [obj[@"id"] isEqualToString:@"3091248962"];}]; if (index == NSNotFound){ NSLog(@"Not found"); } else { NSLog(@"Index Found: %@", @(index)); }`. – Larme May 30 '17 at 09:54

2 Answers2

0
int index = -1;     
for (int i=0; i< [arrJSON count]; i++){
    NSDictionary *dict = [arrJSON objectAtIndex:i];

    if ([[dict objectForKey:@"id"] isEqualToString:@"123456"]){
       NSLog(@"Found at index: %d",i);
       index = i;
       break;
    }
}
if (index == -1){
    NSLog(@"ID not found");
}
Pramod Tapaniya
  • 1,228
  • 11
  • 30
-2
BOOL isIDPresent = NO;
NSInteger index = 0;
for(; index < songsArray.count; index++) {
   NSDictionary *songDict = [songsArray objectAtIndex:index];
   if ([sondDict objectForKey:@"id"] == songId) {
      isIDPresent = YES;
      break;
  }
}
if (isIDPresent == YES) 
   NSLog(@"ID found at index : %d",index);
else
   NSLog(@"ID NOT FOUND");

where songsArray is the NSMutableArray and songId is the id you want to search for. The above code is valid when id is not a string. Check the code below if id is NSString

BOOL isIDPresent = NO;
NSInteger index = 0;
for(; index < songsArray.count; index++) {
   NSDictionary *songDict = [songsArray objectAtIndex:index];
   if ([[sondDict objectForKey:@"id"] isEqualToString:songId]) {
      isIDPresent = YES;
      break;
  }
}
if (isIDPresent == YES) 
   NSLog(@"ID found at index : %d",index);
else
   NSLog(@"ID NOT FOUND");
Arun
  • 1,391
  • 1
  • 10
  • 29
  • i think for loop is not proper way. – Pramod Tapaniya May 29 '17 at 10:16
  • Sorry for the mistake. Thanks @PramodTapaniya – Arun May 29 '17 at 10:22
  • 1
    In objective C, string comparission is not done by `==` operator – Saheb Roy May 29 '17 at 12:14
  • @SahebRoy From the data given in question, id doesn't looks like a string. That is why I gave == If it is a string you can use [[sondDict objectForKey:@"id"] isEqualToString: songId] instead – Arun May 29 '17 at 12:16
  • Edit your answer accordingly, as OP might find it usefull. – Saheb Roy May 29 '17 at 12:20
  • @Arun `[sondDict objectForKey:@"id"]` is an object, since you can only put objects into a NSDictionary. So `if ([[sondDict objectForKey:@"id"] isEqual: songId])` at least? – Larme May 29 '17 at 13:28
  • 1
    This algorithm performs *two* passes over the array - `for` and `indexOfObject:`- when only one is required. – CRD May 29 '17 at 17:53
  • Sorry but your edited version will always return that the item was found provided the array is non-empty, even if the item is not in the array. – CRD May 30 '17 at 08:31