你肯定可以使用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:
导致文本是正确的那里,然后解析。在这种情况下,应该没有任何问题,但通常情况下,您可以预先创建谓词并在相关时刻仅提供参数,如果您最终在真正时间关键的区域使用该参数。