Browse Questions
  • Inside a ScrollView, SwiftUI's vertical scroll direction makes height unconstrained.
  • All three transform collections but handle the results differently.
  • | Problem | Cause | Fix | |---------|-------|-----| | #if SIT always falls through to #else | Flag added to Other Swift Flags instead of Active Compilation Conditions | Move flag to the correct…
  • XCTestExpectation pauses a test until an async condition is fulfilled or a timeout is reached.

Answer: All three transform collections but handle the results differently.

FunctionUse case
mapTransform each element, keep all results
compactMapTransform and remove nil results
flatMapTransform and flatten nested collections

Code Example:

let numbers = [1, 2, 3, 4]

// map — transforms every element
let doubled = numbers.map { $0 * 2 }
// [2, 4, 6, 8]

// compactMap — removes nils
let strings = ["1", "two", "3", "four"]
let integers = strings.compactMap { Int($0) }
// [1, 3]

// flatMap — flattens nested arrays
let nested = [[1, 2], [3, 4], [5, 6]]
let flat = nested.flatMap { $0 }
// [1, 2, 3, 4, 5, 6]