我想遍历一个数组,如果值存在,我想返回 TRUE。
struct Loops {
var loopStep: LoopStep
}
struct LoopStep {
var template: [Template]
}
struct Template {
var stepType: String
}
let templates: [Template] = [Template(stepType: "FORM_ONE"), Template(stepType: "FORM_TWO")]
let loopStep = LoopStep(template: templates)
let incompleteSteps = [Loops(loopStep: loopStep)]
我已经使用 reduce
尝试过这个但是不能使这个语法工作
let result = incompleteSteps.reduce(true, $0.loopStep.template.stepType == "FORM_ONE" )
最佳答案
您只需要使用contains(where:)
来获取一个bool
返回值,该返回值指示集合中是否存在与闭包匹配的元素。由于 template
本身也是一个 Array
,如果您想知道一个Loops
数组包含任何 Loops
,其 template
数组包含具有匹配要求的 Template
。
let result = incompleteSteps.contains(where: {$0.loopStep.template.contains(where: {$0.stepType == "FORM_ONE"})})
关于arrays - 遍历数组,如果值存在则返回 TRUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54019045/