2011-10-20 33 views
0

我想提取所有具有前綴「be」的對象,但我只獲取第一個對象,而不是全部來自各種索引。 「數組」包含各種對象,它包含「be」,「become」,「beta」,「be」,「beaver」等對象。這裏有什麼錯誤?Objective-c enumerateUsingBlock問題

當我使用localizedCaseInsensitiveCompare:時,它只顯示兩個「be」,它在「isEqualToString:」方面是正確的,而「數組」實際上包含來自不同索引的兩個「be」。

的代碼如下:

NSString *string [email protected]"be"; 

NSRange range = NSMakeRange(0, 24); 

NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange: range]; 

[array enumerateObjectsAtIndexes:indexSet options: NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger index, BOOL *stop) 

{ 
    //if([obj localizedCaseInsensitiveCompare:string] == NSOrderedSame) 
    if([obj hasPrefix:string]) 

    { 
     NSLog(@"Object Found: %@ at index: %i",obj, index); 

     *stop=YES; 

    } 

} ]; 

回答

3

,因爲你一旦停止循環,你發現通過*stop = YES線單一的結果你只能獲得第一。刪除。

您還應該使用-indexesOfObjectsPassingTest:與您的測試,然後採取返回的索引集並將其傳遞到-objectsAtIndexes:

+0

啊,你是對的!對不起,我的愚蠢問題。順便說一下,你可以給一個'indexesOfObjectsPassingTest:'的例子嗎? – wagashi