Swiftpack.co -  fourplusone/observed-optional-object as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
fourplusone/observed-optional-object
Observe objects in SwiftUI Views which may be nil
.package(url: "https://github.com/fourplusone/observed-optional-object.git", from: "v0.1.1")

ObservedOptionalObject

Swift

Rationale

SwiftUIs @ObservedObject requires that the observed object actually exists. In some cases it's convenient to observe an object which might be nil. In this case, @ObservedOptionalObject can be used.

struct SomeView: View {
    // Instead of
    @ObservedObject var anObject: Model? // Won't work
    
    // use
    @ObservedOptionalObject var anObject: Model?
    
    var body: some View {
        HStack {
            Text("Name")
            if let name = anObject?.name {
                Text(name)
            }
        }
    }
}

Please note, that @ObservedOptionalObject is only useful if your contains content that should be displayed, even when the object is nil. Otherwise the view should be contained within an if let statement: if let obj = obj { SomeView(anObject: obj) }

Installation

This package is available via SwiftPM

dependencies: [
    .package(url: "https://github.com/fourplusone/observed-optional-object.git", 
        .upToNextMinor(from: "0.1.0")
    )
]

License

This project is licensed under the MIT License.

GitHub

link
Stars: 1
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

2 weeks ago
  • [FEAT] iOS Support

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics