Swiftpack.co - lorenzofiamingo/swiftui-shared-object as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by lorenzofiamingo.
lorenzofiamingo/swiftui-shared-object v1.1.1
A new property wrapper for SwiftUI ObservableObject.
⭐️ 66
πŸ•“ 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/lorenzofiamingo/swiftui-shared-object.git", from: "v1.1.1")

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:

@SharedObject("A") var letterA = Letter()

If you are sure that the object is already been created you can just retrieve the shared object:

@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:

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 AsyncButton πŸ–²οΈ

SwiftUI MapItemPicker πŸ—ΊοΈ

SwiftUI PhotosPicker πŸŒ‡

SwiftUI CachedAsyncImage πŸ—ƒοΈ

SwiftUI VerticalTabView πŸ”

GitHub

link
Stars: 66
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

SwiftUI SharedObject 2.0.0
2 years ago

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