Swiftpack.co - Package - Amzd/PublishedObject
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

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

Releases

Added PublishedObject.projectedValue - 2021-01-04T16:27:30

#5

Renamed to PublishedObject - 2021-01-02T11:37:07

#1

- 2020-12-27T21:31:29

- 2020-12-26T23:10:12

- 2020-12-09T13:59:50