Swiftpack.co -  Amzd/PublishedObject as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Amzd/PublishedObject
A property wrapper that forwards the objectWillChange of the wrapped ObservableObject to the enclosing ObservableObject's objectWillChange.
.package(url: "https://github.com/Amzd/PublishedObject.git", from: "0.2.0")

PublishedObject

A property wrapper that forwards the objectWillChange of the wrapped ObservableObject to the enclosing ObservableObject's objectWillChange.

Just like @Published this sends willSet events to the enclosing ObservableObject's ObjectWillChangePublisher but unlike @Published it also sends the wrapped value's published changes on to the enclosing ObservableObject.

class Outer: ObservableObject {
    @PublishedObject var innerPublishedObject: Inner
    @Published var innerPublished: Inner

    init(_ value: Int) {
        self.innerPublishedObject = Inner(value)
        self.innerPublished = Inner(value)
    }
}

class Inner: ObservableObject {
    @Published var value: Int

    init(_ int: Int) {
        self.value = int
    }
}

func example() {
    let outer = Outer(1)
    
    // Setting property on Outer (This will send an update with either @Published or @PublishedObject)
    outer.innerPublishedObject = Inner(2) // outer.objectWillChange will be called 
    outer.innerPublished = Inner(2)       // outer.objectWillChange will be called

    // Setting property on Inner (This will only send an update when using @PublishedObject)
    outer.innerPublishedObject.value = 3  // outer.objectWillChange will be called !!!
    outer.innerPublished.value = 3        // outer.objectWillChange will NOT be called 
}

It's only one file so you could just copy it. Also has Swift Package Manager support.

GitHub

link
Stars: 9
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.

Release Notes

Optional support
2 weeks ago

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