我有一個對象數組。每個對象都有自己的屬性(名稱,desc,狀態)。 name
和desc
是NSString
和status
是BOOL
。使用自定義對象屬性過濾器數組
我想通過status屬性過濾這個數組。例如:使用status == YES
獲取所有對象。
我該如何做到這一點?
我有一個對象數組。每個對象都有自己的屬性(名稱,desc,狀態)。 name
和desc
是NSString
和status
是BOOL
。使用自定義對象屬性過濾器數組
我想通過status屬性過濾這個數組。例如:使用status == YES
獲取所有對象。
我該如何做到這一點?
嘗試使用NSPredictate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %@", @"YES"];
NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];
這給你所有的對象,其中的地位是平等是一個數組。
謝謝,它的工作原理和答案是第一次。 – erekle
試試這個,
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"status = YES"];
NSArray *filteredArray = [yourArray filteredArrayUsingPredicate:predicate];
爲什麼要複製其他答案? – Ilario
這不是複製的答案。 – Akhilrajtr
您可以發佈正確的答案,但請檢查是否有人像您一樣發帖。然後張貼您的答案或給一些更多的描述... – Mani
你好你可以試試這個,
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" }
感謝
這並不回答這個問題。 – vikingosegundo
它幫助,如果你提供的,你已經嘗試哪些細節。 – Amar
@Kumar,感謝您的編輯。我會預見下一次。 – erekle