`Republished` is a property wrapper enabling nested ObservableObjects in SwiftUI.

Related tags

Utility Republished
Overview

Republished

The @Republished proprty wrapper allows an ObservableObject nested within another ObservableObject to naturally notify SwiftUI of changes.

It was inspired by @mergesort's blog post introducing Boutique.

Problem

Nested ObservableObjects don't play well with SwiftUI.

An ObservableObject is a reference type, not a value type. This means a field on an outer ObservableObject containing an inner ObservableObject doesn't change when the inner object's one's changes. As such the outer object will not send the objectWillChange notification required for SwiftUI to know to rerender views that depend on its data.

Nested ObservableObjects are often a sign your data model needs some refactoring — but it can also be a nice way to separate code concerns. Boutique provides a data store as a nestable ObservableObject. This repo's Example App uses nested ObservableObjects to separate its 'DomainModel' from its 'ViewModel', showcasing a version of the MVVM pattern that separates view-display logic from business logic (and is a bit closer to its original form).

Usage

The outer ObservableObject should hold the inner one in a var annotated with the @Republished property wrapper.

@Republished private var inner: InnerObservableObject

The inner ObservableObject's objectWillChange notifications will be re-emitted by the outer ObservableObject, allowing it to provide SwiftUI compatible accessors derived from the inner.

var infoFromInner: String { "\(inner.info)" }

Note: The outer ObservableObject will only republish notifications from inner ObservableObjects that it actually accesses.

SPM

You can use this library via Swift Package Manger by adding a dependency in your Package.swift.

.package(url: "https://github.com/adam-zethraeus/Republished", from: "0.1.0")

Example App

The RepublishedTestApp contains a simple example of an inner ObservableObject, used by an outer ObservableObject view model, to provide data for a regular SwiftUI View.

You might also like...
Swift-ndi - Swift wrapper around NewTek's NDI SDK

swift-ndi Swift wrapper around NewTek's NDI SDK. Make sure you extracted latest

Coz profiler Swift wrapper/bindings

Swift Coz (Linux Only) Coz profiler Swift wrapper/bindings. This is a Swift wrapper around the Coz profiler here: https://github.com/plasma-umass/coz

An unofficial wrapper around FSEvent tailored for Swift 5.

EonilFSEvents Eonil 2018 Maintenance. 2019 Maintenance. It's possible to use FSEvents directly in Swift, but it still involves many boilerplate works

A swift wrapper for NSTimer

Every.Swift - A Convenient NSTimer Wrapper Usage Provided that your class implement TimerManageable protocol you just do: self.every(3.seconds) {

A Swift wrapper for URL bookmarks which allow a file to be located regardless of whether it is moved or renamed.

Bookmark A Swift wrapper for URL bookmarks which allow a file to be located regardless of whether it is moved or renamed. This class wraps Swift's URL

Unit-Converter-SwiftUI - A simple Unit Converter iOS app built in the process of learning SwiftUI
Unit-Converter-SwiftUI - A simple Unit Converter iOS app built in the process of learning SwiftUI

SwiftUI-Unit-Converter A simple Unit Converter iOS app built in the process of l

Recreating a fully functional version of iOS 4 in SwiftUI.
Recreating a fully functional version of iOS 4 in SwiftUI.

Updates: Version 1.0 is currently available 🎉 While I work on fixing an issue in Xcode 12.5+ with LazyVGrid, I recommend you build using Xcode 12.4 a

I needed to detect idle mode on a SwiftUI app for a kiosk, so I did it.

Inactivity I needed to detect idle mode on a SwiftUI app for a kiosk, so I did it. Usage Important: You must set the Principal class of your app to In

SnippetsLibrary - Code snippets library for SwiftUI Devs.
SnippetsLibrary - Code snippets library for SwiftUI Devs.

SnippetsLibrary is a helpful tool for SwiftUI developers to help with their daily coding life. SnippetsLibrary contains all the needed code snippets for you to view, edit, or add more and more. This will make your daily work easier and faster.

Comments
  • How can `Binding` vars be used?

    How can `Binding` vars be used?

    Hey, thanks for contributing this library! It's helped partly solve a problem I faced when having nested Input/Output objects within my view model (mostly for the sakes of nice namespacing). For example:

    final class ViewModel: ObservableObject {
      var input: Input
      @Republished var output: Output
      
      class Output: ObservableObject {
        @Published var test: Bool
      }
    }
    
    struct ContentView: View {
      @ObservedObject var viewModel: ViewModel
    }
    

    However, one problem I have faced is if I have a @Published var within an ObservableObject (in the above example test) and that object is held as a @Republished var, then I'm no longer able to inject that into a view and make a @Binding var from it as it throws a get-only compile error. This was previously possible when using a @Published property wrapper.

    Is there any way around this? Happy to elaborate on this if what I'm trying to explain isn't clear.

    Thanks!

    opened by lawmaestro 5
  • [breaking] Make Republished vars binding

    [breaking] Make Republished vars binding

    1. make the projected value of a @Republished var be Binding
    2. this breaks existing behavior—which was to use that accessor to expose internals.
    3. the new way is better
    4. also add tests.
    opened by adam-zethraeus 0
Releases(v1.0.0)
Owner
Adam Zethraeus
Adam Zethraeus
A Swift property wrapper which stores the previous value

swift-with-previous A Swift property wrapper which stores the previous value. The previous value can be get by the projected value $propertyName. impo

IKEDA Sho 3 Feb 22, 2022
StoredIn is a simple property wrapper library to store any value in anywhere

StoredIn StoredIn is a simple property wrapper library to store any value in anywhere. Installation Please use the Swift Package Manager. dependencies

Henrique Sasaki Yuya 2 Jul 4, 2022
μ-library enabling if/else and switch statements to be used as expressions.

swift-expression Many languages such as Scala, Rust and Kotlin support using if/else and switch statements as expressions – meaning that they can by t

Nikita Mounier 1 Nov 8, 2021
Swift Property Wrappers, but in Objective-C. And done horribly.

TOPropertyAccessor is an open source, Objective-C abstract class. Similar to Realm's Cocoa API, it uses the dynamic nature of the Objective-C runtime to access the properties of any of its subclasses, and routes calling them through overridable access points.

Tim Oliver 3 May 23, 2021
A Collection of useful Swift property wrappers to make coding easier

Swift Property Wrappers A Collection of useful Swift property wrappers to make c

Gordan Glavaš 2 Jan 28, 2022
💡 A light Swift wrapper around Objective-C Runtime

A light wrapper around Objective-C Runtime. What exactly is lumos? lumos as mentioned is a light wrapper around objective-c runtime functions to allow

Suyash Shekhar 139 Dec 19, 2022
UTIKit is an UTI (Uniform Type Identifier) wrapper for Swift.

UTIKit UTIKit is an UTI (Uniform Type Identifier) wrapper for Swift. Features UTIKit is a full featured library including entire UTI functions. Conver

Hiroki Kato 254 Nov 27, 2022
Swift wrapper for PuLP module.

SwiftPuLP Wraps Python Linear Programming PuLP module. Requirements Requires PuLP to be installed, and may require PYTHON_LIBRARY environment variable

Michel Tilman 0 Dec 24, 2021
Dungeon Crawl: Stone Soup iOS Webtiles wrapper

dcss-ios Dungeon Crawl: Stone Soup iOS Webtiles wrapper Summary This project aims to make DCSS webtiles a joy to play on any iOS device without an ext

Jon Lazar 2 Aug 10, 2022
Contacts wrapper for iOS 9 or upper with Objective-C

ContactsWrapper Contacts wrapper for iOS 9 or upper with Objective-C. For the information translated to Russian, take a look at this link. Requirement

Abdullah Selek 22 Jun 18, 2022