Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tunous/optionalenvironmentobject
Safe way to access SwiftUI EnvironmentObject
https://github.com/tunous/optionalenvironmentobject
Last synced: about 1 month ago
JSON representation
Safe way to access SwiftUI EnvironmentObject
- Host: GitHub
- URL: https://github.com/tunous/optionalenvironmentobject
- Owner: Tunous
- License: mit
- Created: 2024-07-22T17:11:16.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-07-23T16:20:10.000Z (4 months ago)
- Last Synced: 2024-09-24T12:16:06.496Z (about 1 month ago)
- Language: Swift
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OptionalEnvironmentObject
A way to access SwiftUI EnvironmentObject in a safe way without crashing application.
## Usage
```swift
import SwiftUI
import OptionalEnvironmentObjectfinal class MyObject: ObservableObject {}
struct ContentView: View {
@EnvironmentObject.Optional private var object: MyObject?
var body: some View {
if let object {
Text("Found: \(object)")
} else {
Text("Not found")
}
}
}#Preview {
VStack {
ContentView()
ContentView()
.optionalEnvironmentObject(MyObject())
}
}
```## Installation
### Swift Package Manager
Add the following to the dependencies array in your "Package.swift" file:
```swift
.package(url: "https://github.com/Tunous/OptionalEnvironmentObject", .upToNextMajor(from: "1.0.0"))
```Or add https://github.com/Tunous/OptionalEnvironmentObject, to the list of Swift packages for any project in Xcode.