Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lorenzofiamingo/swiftui-shared-object
A new property wrapper for SwiftUI ObservableObject.
https://github.com/lorenzofiamingo/swiftui-shared-object
propertywrapper swift swiftui
Last synced: 3 months ago
JSON representation
A new property wrapper for SwiftUI ObservableObject.
- Host: GitHub
- URL: https://github.com/lorenzofiamingo/swiftui-shared-object
- Owner: lorenzofiamingo
- License: mit
- Created: 2020-07-31T15:42:54.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-21T13:08:20.000Z (8 months ago)
- Last Synced: 2024-07-10T14:34:54.300Z (6 months ago)
- Topics: propertywrapper, swift, swiftui
- Language: Swift
- Homepage:
- Size: 29.3 KB
- Stars: 66
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swiftui - lorenzofiamingo/SharedObject - π± A new property wrapper for SwiftUI `ObservableObject`. (Samples)
README
# SwiftUI SharedObject π±
`@SharedObject` is an alternative to `@StateObject`, `@ObservedObject`, `@EnvironmentObject` to handle `ObservableObject`.
If you need to have multiple objects of the same class persisted among multiple view instances, it's difficult to handle the situation with other wrappers: with `@StateObject` the object will deinit with the view and be generated only for the specific view instance, with `@EnvironmentObject` you can bind only one instance of the same class for each Environment and with `@ObservedObject` is difficult to propagate object in nested views.`@SharedObject` simply stores the objects using an identifier, so you can retrieve it each time you'll need it.
## Usage
Retrieve the shared object with the given id or, if not present, create a shared object with an initial value:
```swift
@SharedObject("A") var letterA = Letter()
```
If you are sure that the object is already been created you can just retrieve the shared object:
```swift
@SharedObject("A") var letterA: Letter
```
You can give a default initial value to the class, so you don't need to specify in each view you think the object will be created:
```swift
final class Letter: SharableObject {var value: String
init(_ value: String) {
self.value = value
}
static var initialValue: Self {
.init("A")
}
}
```## Installation
1. In Xcode, open your project and navigate to **File** β **Swift Packages** β **Add Package Dependency...**
2. Paste the repository URL (`https://github.com/lorenzofiamingo/swiftui-shared-object`) and click **Next**.
3. Click **Finish**.## Other projects
[SwiftUI VariadicViews π₯](https://github.com/lorenzofiamingo/swiftui-variadic-views)
[SwiftUI AsyncButton π²οΈ](https://github.com/lorenzofiamingo/swiftui-async-button)
[SwiftUI MapItemPicker πΊοΈ](https://github.com/lorenzofiamingo/swiftui-map-item-picker)
[SwiftUI PhotosPicker π](https://github.com/lorenzofiamingo/swiftui-photos-picker)
[SwiftUI CachedAsyncImage ποΈ](https://github.com/lorenzofiamingo/swiftui-cached-async-image)
[SwiftUI VerticalTabView π](https://github.com/lorenzofiamingo/swiftui-vertical-tab-view)