Jim Clermonts
Jim Clermonts

Reputation: 2650

Kotlin how to call method when all Boolean values are true in BooleanArray

When all booleans are set to true and I run startProcess() then it should be true and I want to execute the onFinished() method. How do I do this?

private var completed: BooleanArray = booleanArrayOf(false, false, false, false)

fun startProcess() {
    completed.all { it -> callback.onFinished() }
}

Upvotes: 1

Views: 2890

Answers (2)

Benjamin
Benjamin

Reputation: 7368

Just use:

private var completed: BooleanArray = booleanArrayOf(false, false, false, false)
if (completed.all { it }) {
    callback.onFinished()
}

Upvotes: 1

Roland
Roland

Reputation: 23242

Put the all in an if-condition, e.g.:

fun startProcess() {
  if (completed.all { it })
    callback.onFinished()
}

from the linked all-reference:

Returns true if all elements match the given predicate.

Upvotes: 6

Related Questions