SwiftUI > IfLet is an utility library that adds back a way to use the if let
functionality on SwiftUI. Also, it adds a new funcionality, equivalent to using if let
on Binding
objects
Because of the nature of this feature, this has the same requirements as SwiftUI:
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
This can only be used (for now) as a SwiftPM package.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
You can follow the instructions here to learn how to add a Swift Package to your project from Xcode.
Once you have your Swift package set up, add this to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/baguio/SwiftUI-IfLet.git", from: "0.3.0")
]
IfLet and IfBindingLet can only build one View at a time, but this view can be a View group (Groups, Stacks, etc)
let foo : String? = nil
VStack {
IfLet(foo) { bar in
VStack {
Text("This won't appear because the variable used is nil")
Text(bar)
}
}
IfLet(foo, { bar in
Text(bar)
}, else: {
Text("This text will appear, since the variable used is nil")
})
}
@State var foo : String? = nil
VStack {
IfBindingLet($foo) { bar in
VStack {
Text("This won't appear because the variable used is nil")
TextField("Input", text: bar)
}
}
IfBindingLet($foo, { bar in
TextField("Input", text: bar)
}, else: {
Text("This text will appear, since the variable used is nil")
})
}
SwiftUI-IfLet is released under the MIT license. See LICENSE for details.