gpt4 book ai didi

ios - 如何快速创建过滤器数组

转载 作者:行者123 更新时间:2023-11-29 00:52:56 25 4
gpt4 key购买 nike

如何在 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

这就是我要找的,有没有可用的解决方案?请指导我找到合适的解决方案。

最佳答案

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

// 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

关于ios - 如何快速创建过滤器数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37921018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com