我将如何遍历NSMuttableArray,以检查ID是否与特定用户ID相同。?

时间:2017-05-29 10:06:47

标签: ios objective-c nsmutablearray

我想要一个循环概念,以便查找我的代码中的id是否与array中已存在的id匹配或相同。(例如,我想检查id 12345是否存在于关注NSMutableArray) 并且还想知道它发生在哪个索引路径以及如何更改

{
    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;
}

2 个答案:

答案 0 :(得分:0)

click

答案 1 :(得分:-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");

其中songsArray是NSMutableArray,songId是您要搜索的ID。当id不是字符串时,上面的代码有效。如果id是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");