Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fourplusone/observed-optional-object
Observe objects in SwiftUI Views which may be nil
https://github.com/fourplusone/observed-optional-object
ios macos swift swift-library swiftui tvos watchos
Last synced: about 1 month ago
JSON representation
Observe objects in SwiftUI Views which may be nil
- Host: GitHub
- URL: https://github.com/fourplusone/observed-optional-object
- Owner: fourplusone
- License: mit
- Created: 2021-08-24T18:40:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-21T16:11:35.000Z (over 3 years ago)
- Last Synced: 2024-11-13T12:48:16.254Z (about 2 months ago)
- Topics: ios, macos, swift, swift-library, swiftui, tvos, watchos
- Language: Swift
- Homepage:
- Size: 3.91 KB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ObservedOptionalObject
[![Swift](https://github.com/fourplusone/observed-optional-object/actions/workflows/swift.yml/badge.svg)](https://github.com/fourplusone/observed-optional-object/actions/workflows/swift.yml)
## Rationale
`SwiftUI`s `@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.```swift
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
```swift
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.