A new property wrapper for SwiftUI ObservableObject.

Overview

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, its 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 FileSwift PackagesAdd Package Dependency...
  2. Paste the repository URL (https://github.com/lorenzofiamingo/SwiftUI-SharedObject) and click Next.
  3. Click Finish.

Other projects

CachedAsyncImage 🗃️

VerticalTabView 🔝

You might also like...
A Quick macOS app that will check a website's sitemap.xml against a new domain
A Quick macOS app that will check a website's sitemap.xml against a new domain

Migrator Quick and dirty native macOS app that will check a website's sitemap.xml against a new domain. When you're developing your new website and wa

The purpose of this app is to list all of the schools in New Jersey and assign ratings to chosen schools.
The purpose of this app is to list all of the schools in New Jersey and assign ratings to chosen schools.

My First iOS App Introduction This is my first iOS App from my CSSE337 Enterprise Mobile Apps class for college. Here was the objective of the assignm

Unidirectional reactive architecture using new Apple Combine framework
Unidirectional reactive architecture using new Apple Combine framework

Unidirectional Reactive Architecture. This is a Combine implemetation of ReactiveFeedback and RxFeedback

Ios-exercise - In this exercise applicant should implement a new feature into an existing sample app

Cooking app In this exercise applicant should implement a new feature into an ex

ReleaseNotesKit - a brand new, elegant, and extremely simple way to present the recent version’s release notes to your users
ReleaseNotesKit - a brand new, elegant, and extremely simple way to present the recent version’s release notes to your users

ReleaseNotesKit This is ReleaseNotesKit, a brand new, elegant, and extremely simple way to present the recent version’s release notes to your users. R

Trivia about Animal Crossing: New Horizons. My first iOS (iPhone) app.

ACNH-trivia-iOS-app Trivia about Animal Crossing: New Horizons. This is my first iOS (iPhone) app. I am excited to start my journey as an iOS develope

Booky heavily-commented demo app built to explore Apple's new 'App Intents' framework introduced in iOS 16
Booky heavily-commented demo app built to explore Apple's new 'App Intents' framework introduced in iOS 16

Booky Demo App ℹ️ ABOUT Booky is a work-in-progress, heavily-commented demo app built to explore Apple's new 'App Intents' framework introduced in iOS

WeHobby is an IOS application which aims to facilitate the discovery of new hobbies while creating links between users.

🇬🇧 WeHobby (EN) WeHobby is an IOS application which aims to facilitate the discovery of new hobbies while creating links between users. The issues t

New version of CardinalKit based on the Swift Package Manager and a modularized architecture.

CardinalKit Open-source framework for rapid development of modern, interoperable digital health applications. Contributing Contributions to this proje

Comments
  • Odd thing in readme

    Odd thing in readme

    "The problem is that with @StateObject the object will deinit with the view"

    That is its main advantage so I'm intrigued to know why it's causing a problem for you.

    opened by malhal 8
  • Thanks for this wonderful sharedObject

    Thanks for this wonderful sharedObject

    As I mentioned on title, thank you for sharing this wonderful repo, and I'm currently using it in my code very happly. However I found that there's a part that printing the typeof ObjectType everytime I interact with it. Could you please remove the 15th line on SharedRepository.swift

    enhancement 
    opened by rnjstjddn96 1
Releases(2.0.0)
Owner
Lorenzo Fiamingo
Lorenzo Fiamingo
Short examples and references to work with property wrappers in SwiftUI.

A Colection of useful playgrounds This repo contains a collection of useful swift playgrounds. PropertyWrappers Playground Contains small example of u

Joan F. Guerrero 0 Nov 24, 2021
Animal Crossing New Horizon companion app in SwiftUI

You can now download the app on the App Store! The best Animal Crossing New Horizons companion app! 最高の「あつまれ どうぶつの森」コンパニオンアプリ Animal Crossing New Hori

Thomas Ricouard 1.6k Dec 29, 2022
Beers is a simple experimental app implemented using the new amazing SwiftUI.

Beers is a simple experimental app implemented using the new amazing SwiftUI. The app shows a list of beers fetched from Punk API

Chris 27 Dec 20, 2022
Basic Todo list application built using the new SwiftUI framework and Core Data

Dub Dub Do - A sample TODO List Application in SwiftUI Updated for Xcode 11.5 This is a basic app that lets you create a list of todos, mark them as i

Stephen McMillan 67 Sep 28, 2022
A simple todo app which aims to demonstrate some new concepts from SwiftUI 3

TodoAppSwiftUI3 A simple todo app which aims to demonstrate some new concepts from SwiftUI 3 Description This is a simple todo app built to demonstrat

Roman Luzgin 50 Nov 1, 2022
Examples of new SwiftUI framework

SwiftUI by Examples SwiftUI is a simple way to build user interfaces across all Apple platforms with the power of Swift. I'm going to fill the repo wi

Artem Novichkov 192 Dec 9, 2022
Show What's New with SwiftUI.

SwiftNEWKit Accelerated by Apple SwiftUI Aims Provide an easy way for Apple Developers to Show "What's New" to the end users. Features Auto trigger th

MING 46 Jan 3, 2023
An iPhone Simulator "Wrapper" for SwiftUI Apps on macOS

SwiftUIPhone Run a SwiftUI app (or any SwiftUI view) in an iPhone Simulator "wrapper", directly on macOS! To be clear, this is not an iPhone Simulator

Justin Kaufman 7 May 20, 2022
A SwiftUI wrapper around the `Add to Siri` button used to donate INIntents to the system.

AddToSiri A SwiftUI wrapper around the Add to Siri button used to donate INIntents to the system. Originally created by Reddit user u/dippnerd (Github

Florian Schweizer 5 Nov 23, 2022
An example project to demonstrate the new scripting capabilities of Shortcuts For Mac.

Scripting Shortcuts Test Project This simple project is designed to test the new scripting capabilities introduced in the Shortcuts app in macOS Monte

Alex Hay 16 Nov 17, 2022