Created
February 7, 2023 17:03
-
-
Save andresr-dev/799f7b7295e437f2c6f7221efa084851 to your computer and use it in GitHub Desktop.
This is a custom operator that allows you to perform nil coalescing with optional bindings in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
public func ??<T>(lhs: Binding<Optional<T>>, rhs: T) -> Binding<T> { | |
Binding( | |
get: { lhs.wrappedValue ?? rhs }, | |
set: { lhs.wrappedValue = $0 } | |
) | |
} | |
// MARK: - USE | |
struct ContentView: View { | |
@State private var name: String? | |
var body: some View { | |
TextField("Name", text: $name ?? "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment