I have this check in SwiftUI
if tasks.first(where: { task in
return isDone(task)
}) != nil {
let task = tasks.first(where: { task in
return isDone(task)
}
)
I'm wondering if there's a way in SwiftUI 3 to use task
from the check, instead of having to redefine it in the success block?
I've tried replacing the first line with if let task = tasks.first ....
but it returns a Bool, which makes sense.
Not really sure what this is called so don't know what else to research.
Thank you!
CodePudding user response:
I assume you wanted something like this (simplified):
struct Task {
var isDone: Bool {
true
}
var name = "test"
}
func foo() {
let tasks = [Task(), Task()]
// just don't use `!= nil` converting it to Bool
if let task = tasks.first(where: { $0.isDone }) {
print(task.name)
}
}