你肯定可以使用NSPredicate
這個。大概最容易做的事情是將用於所有四個開關相同IBAction
,並讓它做一個重新計算:
- (IBAction)anySwitchDidChange:(id)sender
{
// make a set of all acceptable call types
NSMutableSet *acceptableCallTypes = [NSMutableSet set];
if(self.fSwitch.on) [acceptableCallTypes addObject:@"f"];
// ... etc, to create acceptableCallTypes and acceptableCounties
NSPredicate *predicate =
[NSPredicate predicateWithFormat:
@"(%@ contains callType) and (%@ contains county)",
acceptableCallTypes, acceptableCounties];
/*
this predicate assumes your objects have the properties 'callType' and
'county', and that you've filled the relevant sets with objects that would
match those properties via isEqual:, whether strings or numbers or
anything else.
NSDictionaries are acceptable since the internal mechanism used here is
key-value coding.
*/
NSArray *filteredArray = [_sourceArray filteredArrayUsingPredicate:predicate];
// send filteredArray to wherever it needs to go
}
使用predicateWithFormat:
導致文本是正確的那裏,然後解析。在這種情況下,應該沒有任何問題,但通常情況下,您可以預先創建謂詞並在相關時刻僅提供參數,如果您最終在真正時間關鍵的區域使用該參數。