2014-02-05 86 views
1

我有一個對象數組。每個對象都有自己的屬性(名稱,desc,狀態)。 namedescNSStringstatusBOOL使用自定義對象屬性過濾器數組

我想通過status屬性過濾這個數組。例如:使用status == YES獲取所有對象。

我該如何做到這一點?

+0

它幫助,如果你提供的,你已經嘗試哪些細節。 – Amar

+0

@Kumar,感謝您的編輯。我會預見下一次。 – erekle

回答

8

嘗試使用NSPredictate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %@", @"YES"]; 
NSArray *filterArray = [array filteredArrayUsingPredicate:predicate]; 

這給你所有的對象,其中的地位是平等是一個數組。

+0

謝謝,它的工作原理和答案是第一次。 – erekle

1

試試這個,

NSPredicate *predicate =[NSPredicate predicateWithFormat:@"status = YES"]; 
NSArray *filteredArray = [yourArray filteredArrayUsingPredicate:predicate]; 
+0

爲什麼要複製其他答案? – Ilario

+0

這不是複製的答案。 – Akhilrajtr

+0

您可以發佈正確的答案,但請檢查是否有人像您一樣發帖。然後張貼您的答案或給一些更多的描述... – Mani

-1

你好你可以試試這個,

NSMutableArray *array =[NSMutableArray arrayWithObjects:@"Apple", @"Animal", @"baby", @"ball", nil]; 

NSPredicate *aPredicate = 
    [NSPredicate predicateWithFormat:@"SELF beginswith[a] 'b'"]; 
NSArray *beginWithA = 
    [array filteredArrayUsingPredicate:aPredicate]; 

The beginWithA array will have { @"Apple", @"Animal" }. 

NSPredicate *bPredicate = 
    [NSPredicate predicateWithFormat:@"SELF contains[b] 's'"]; 
[array filterUsingPredicate:bPredicate]; 
The array will have { @"baby", @"ball" } 

感謝

+0

這並不回答這個問題。 – vikingosegundo

相關問題