Ios 如何在swift中创建过滤器数组

Ios 如何在swift中创建过滤器数组,ios,swift,Ios,Swift,如何在swift中创建过滤器变量或多个过滤器? 例如: let i2 = $0.bCondition == "nonac" let i3 = $0.aCondition == "nonac" let i4 = $0.cCondition == "nonac" if <some condition>{ let i5 = $0.vCondition == "nonac" let i6 = $0.mCondition == "nonac" } final filter = i2+

如何在swift中创建过滤器变量或多个过滤器? 例如:

let i2 = $0.bCondition == "nonac"
let i3 = $0.aCondition == "nonac"
let i4 = $0.cCondition == "nonac"
if <some condition>{
  let i5 = $0.vCondition == "nonac"
  let i6 = $0.mCondition == "nonac"
 }
final filter = i2+i3+i4+i6+i5
让i2=$0.b条件==“nonac”
设i3=$0.a条件==“nonac”
设i4=$0.cCondition==“nonac”
如果{
设i5=$0.V条件==“nonac”
设i6=$0.mCondition==“nonac”
}
最终滤波器=i2+i3+i4+i6+i5

这就是我要找的,有什么解决方案吗?请指导我找到合适的解决方案。

一种方法是将
条件
条件
等字段转换为
字典
。从那里,您可以从字典中获取
,并执行一些简单的集合运算:

struct Struct {
    let dict : [String : String]
}

let struct1 = Struct(dict: [
    "aCondition" : "a",
    "bCondition" : "b",
    "cCondition" : "c"
])

let struct2 = Struct(dict: [
    "aCondition" : "a",
    "bCondition" : "b",
    "cCondition" : "nonac"
])

let structs = [struct1, struct2] //structs to be filtered

let filters : Set = ["cCondition"] //the keys to be checked for "nonac"

let filtered = structs.filter{ //filter all structs
    let dict = $0.dict //get the dict of keys from the current iteration
    let keys = Set(dict.keys) //make a set from the dict keys
    let keysToCheck = keys.intersection(filters) //filter out to get the keys we want

    let values = keysToCheck.map{dict[$0]} //get the values for the keys to check

    return values.contains{$0 != "nonac"} //only keep if value != "nonac"
}

print(filtered)print(output)

一种方法是将
a条件
b条件
等字段转换为
字典
。从那里,您可以从字典中获取
,并执行一些简单的集合运算:

struct Struct {
    let dict : [String : String]
}

let struct1 = Struct(dict: [
    "aCondition" : "a",
    "bCondition" : "b",
    "cCondition" : "c"
])

let struct2 = Struct(dict: [
    "aCondition" : "a",
    "bCondition" : "b",
    "cCondition" : "nonac"
])

let structs = [struct1, struct2] //structs to be filtered

let filters : Set = ["cCondition"] //the keys to be checked for "nonac"

let filtered = structs.filter{ //filter all structs
    let dict = $0.dict //get the dict of keys from the current iteration
    let keys = Set(dict.keys) //make a set from the dict keys
    let keysToCheck = keys.intersection(filters) //filter out to get the keys we want

    let values = keysToCheck.map{dict[$0]} //get the values for the keys to check

    return values.contains{$0 != "nonac"} //only keep if value != "nonac"
}

print(filtered)print(output)

从外观上看,您不仅需要动态添加过滤器,还需要动态评估它们。为此,您可以使用闭包和一些声明在其上操作的运算符:

// Closure that takes no arguments and returns Bool
typealias Filter = () -> Bool

// Convenience operator to combine outputs of two filters with AND operator
func && (lhs: Filter, rhs: Filter) -> Filter {
    return {
        lhs() && rhs()
    }
}

// Convenience operator to combine outputs of two filters with OR operator
func || (lhs: Filter, rhs: Filter) -> Filter {
    return {
        lhs() || rhs()
    }
}
示例:

var foo = "Foo"
let bar = "Bar"
let qux = "Qux"

let filter1: Filter = { foo == "Foo" }
let filter2: Filter = { bar == "Bar" }
let filter3: Filter = { qux == "Qux" }
let compositeFilter = filter1 && filter2 && filter3
//                  ^-- Is this what you are looking for?

let before = compositeFilter()

foo = "FOO"

let after = compositeFilter()

print(before)   // true
print(after)    // false

从外观上看,您不仅需要动态添加过滤器,还需要动态评估它们。为此,您可以使用闭包和一些声明在其上操作的运算符:

// Closure that takes no arguments and returns Bool
typealias Filter = () -> Bool

// Convenience operator to combine outputs of two filters with AND operator
func && (lhs: Filter, rhs: Filter) -> Filter {
    return {
        lhs() && rhs()
    }
}

// Convenience operator to combine outputs of two filters with OR operator
func || (lhs: Filter, rhs: Filter) -> Filter {
    return {
        lhs() || rhs()
    }
}
示例:

var foo = "Foo"
let bar = "Bar"
let qux = "Qux"

let filter1: Filter = { foo == "Foo" }
let filter2: Filter = { bar == "Bar" }
let filter3: Filter = { qux == "Qux" }
let compositeFilter = filter1 && filter2 && filter3
//                  ^-- Is this what you are looking for?

let before = compositeFilter()

foo = "FOO"

let after = compositeFilter()

print(before)   // true
print(after)    // false

a条件
b条件
,等等,似乎有一股代码的味道。你可以把它们编入
字典
,使过滤变得非常简单。你能给我一个代码示例吗?不太好,因为我对你试图解决的问题的性质了解不够。我想添加多个过滤器,但是动态的。这就是问题所在。这并没有告诉我多少关于
a条件
和其他字段的信息,它们是做什么的,以及它们是否可以被重新编入词典
a条件
b条件
,等等。看起来像是一股代码味。你可以把它们编入
字典
,使过滤变得非常简单。你能给我一个代码示例吗?不太好,因为我对你试图解决的问题的性质了解不够。我想添加多个过滤器,但是动态的。这就是问题所在。这并没有告诉我多少关于
a条件
和其他字段的信息,它们是做什么的,以及它们是否可以被改写成字典。这是我寻找的方式,一个复合动态过滤器这是我寻找的方式,一个复合动态过滤器