2017-05-29 50 views
0

我想要一個循環的概念,以便找到我的代碼中的id是否與array中已存在的id匹配或相同(例如,我想檢查id 12345是否存在於以下NSMutableArray) 以及也想知道哪個索引路徑已發生和如何更改我將如何遍歷NSMuttableArray,檢查ID是否與特定用戶ID相同。

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

你可以使用 - > [NSMutableArray的containsObject:YourID]。 –

+1

[使用NSPredicate過濾NSDictionary對象的NSArray]可能的重複(https://stackoverflow.com/questions/10505154/filtering-nsarray-of-nsdictionary-objects-using-nspredicate) –

+0

'indexOfObjectPassingTest:'應該做的伎倆。 – Larme

回答

-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"); 
+0

我認爲循環是不正確的方式。 –

+0

對不起。謝謝@PramodTapaniya – Arun

+0

@Roby檢查我更新的答案 – Arun

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"); 
} 
+1

這也是解決此問題的另一種方法。但是當循環中的條目數量太大時,for循環比謂詞更快。如果你的應用沒有複雜的用戶界面,這兩種方法的工作效果都很好 – Arun

+0

@Pramod Tapaniya .... thnkuu donee ....但我也想得到它發生的索引(索引路徑)的信息。 ..任何答案 – Roby

+0

@阿倫我現在必須使用循環,因爲Roby也想索引。 :) –