Binding+Optional.swift 524 B

123456789101112131415161718192021
  1. //
  2. // Binding+Optional.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2025-04-21.
  6. // Copyright © 2025 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftUI
  10. extension Binding where Value: Equatable {
  11. /// Create a Binding<Value> out of a Binding<Value?> by substituting `defaultValue` when nil.
  12. init(_ source: Binding<Value?>, replacingNilWith defaultValue: Value) {
  13. self.init(
  14. get: { source.wrappedValue ?? defaultValue },
  15. set: { source.wrappedValue = $0 }
  16. )
  17. }
  18. }