Skip to content

Instantly share code, notes, and snippets.

@darrarski
Last active January 7, 2021 13:15
Show Gist options
  • Save darrarski/f3886d4d0a3598132a3fdd62b3061eb2 to your computer and use it in GitHub Desktop.
Save darrarski/f3886d4d0a3598132a3fdd62b3061eb2 to your computer and use it in GitHub Desktop.
SwiftUI IfLet helper functions
import SwiftUI
func IfLet<T, ThenOut: View>(
_ value: T?,
then: (T) -> ThenOut
) -> some View {
ViewBuilder.buildIf(value.map { then($0) })
}
func IfLet<T, ThenOut: View, ElseOut: View>(
_ value: T?,
then: (T) -> ThenOut,
`else`: () -> ElseOut
) -> some View {
value.map { ViewBuilder.buildEither(first: then($0)) }
?? ViewBuilder.buildEither(second: `else`())
}
@filimo
Copy link

filimo commented Feb 17, 2020

Oops, I didn't notice))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment