Skip to content

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

Notifications You must be signed in to change notification settings

lincolnanders5/NestedPublished

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NestedPublished

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 {
    @NestedPublished var innerNestedPublished: Inner
    @Published var innerPublished: Inner

    init(_ value: Int) {
        self.innerNestedPublished = 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 @NestedPublished)
    outer.innerNestedPublished = 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 @NestedPublished)
    outer.innerNestedPublished.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.

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 84.8%
  • Ruby 15.2%