【发布时间】:2019-01-03 08:57:26
【问题描述】:
我想遍历一个数组,如果一个值存在,我想返回 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:)呢? – 比较stackoverflow.com/questions/29679486/… -
Offtopic:如果你有固定值,你应该使用枚举。这样,会容易很多:)
-
不相关:在结构和类名中避免使用复数形式:
Loop而不是Loops