Advanced iOS Design Patterns, Episode 8: Use Composite | Kodeco, the new raywenderlich.com

Practice using the composite pattern in “Defeat Your To-do List”, a to-do list creator app.


This is a companion discussion topic for the original entry at https://www.kodeco.com/1941458-advanced-ios-design-patterns/lessons/8

For those who are curious, as of iOS 8.0, array also implements allSatisfy which is a more efficient way to ensure that every element of the array returns true than reduce.

Reduce will go through every element of the arrays while allSatisfy will stop as soon as it hits the first false value. You can test it in the playground:

var elements: [Bool] = [true, true, true, false, true, true, true]

print("Testing allSatisfy:")
elements.allSatisfy {
  print("Testing element: \($0)")
  return $0 == true
}

print("Testing reduce:")
elements.reduce(true) { result, currentObject in
  print("Testing element: \(currentObject)")
  return result && currentObject
}
1 Like