2014-02-12 45 views
0

的數組我有以下情況:NSPredicate的陣列

NSArray(
    NSArray(
     string1, 
     string2, 
     string3, 
     string4 
    ) 
    , 
    NSArray(
     string1, 
     string2, 
     string3, 
     string4 
    ) 
) 

我需要的是它返回一個包含從objectAtIndex特定字符串數組謂詞:0這是關係到字符串1。我應該找回整個數組,因爲我需要處理該數組內的其他字符串。可能嗎?

什麼,我嘗試了,現在要做的是以下幾點:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"ANY SELF contains[cd] %@",searchText]; 

searchData = [myArray filteredArrayUsingPredicate:resultPredicate]; 
NSLog(@"array %@",searchData);` 

我的問題是,當任何字符串1,字符串,STRING3的,串,4包含SEARCHTEXT和我的代碼將返回數組不僅STRING1和無論如何我無法實現我的目標!

+0

我不明白您的要求,您能否詳細說明呢?如果searchText會「str」,那麼你想得到什麼? – iPatel

+0

你能告訴你想要什麼樣的輸出嗎?一個規範化的數組或什麼? –

回答

0

我的opinion是謂詞通常是一個失敗現在我們有塊,尤其是如果你遇到這樣的問題。你可以得到你想要使用:

NSIndexSet *indexes = 
[myArray indexesOfObjectsPassingTest:^BOOL(NSArray *innerArray, __unused NSUInteger idx, __unused BOOL *stop) { 
    // your test on innerArray here, returning BOOL 
}]; 
NSArray *searchData = [myArray objectsAtIndexes:indexes]; 

或用少許finagling(這不是一個內置的方法):

NSArray *searchData = [myArray objectsPassingTest:^BOOL(NSArray *innerArray) { 
    // your test on innerArray here, returning BOOL 
}]; 
+0

謝謝你,我發現你的代碼對我的應用程序中的其他搜索非常有用,所以我會將你的答案標記爲解決方案:) 感謝您的幫助 –