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.
| Function | Use case |
|---|---|
map | Transform each element, keep all results |
compactMap | Transform and remove nil results |
flatMap | Transform 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]