SwiftUI > IfLet

master

[DEPRECATED by Xcode 12] if let and if binding let for SwiftUI
jhonatn/SwiftUI-IfLet

SwiftUI > IfLet

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

Requirements

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+

Installation

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.

As an app dependency

You can follow the instructions here to learn how to add a Swift Package to your project from Xcode.

As a package dependency

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")
]

Usage

IfLet and IfBindingLet can only build one View at a time, but this view can be a View group (Groups, Stacks, etc)

IfLet

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")
    })
}

IfBindingLet

@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")
    })
}

License

SwiftUI-IfLet is released under the MIT license. See LICENSE for details.

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Oct 17 2024 15:28:49 GMT-0900 (Hawaii-Aleutian Daylight Time)