Just another remote app developer


SwiftUI and Swift Package Manager: Combine! Now with Singletons

I want to build upon previous post. This is where we got to:

⬆️This is the core of the package, and this is my SwiftUI code ⬇️

Cool right! We can now return a phrase from our language package and show it’s value in a Text view 😁

What’s not so great about this is that if I want to access the language package deeper down in my view hierarchy I’m out of luck. If I want to do that, I need to create a new instance of the Language class, and I dont want to do that, since this would require me to do another fetch and a new loading process and the like. It would work, but it would be slow which is not what we are after 🤨

So what do we do now? Insert Singletons! I bet there are other ways to do this, but this is what I did.

So how do we do this? Well, we need our singleton, and this is how I did it

There is almost nothing to it… You just need to declare a public var/let that can take an instance of Language.

Now we want to use this of course. And this requires some minor changes to our ContentView.swift file, and we want to create a new SwiftUI view as a child 👶 :

We now use the public variable containing our singleton as the @ObjectBinding variable. But we still need to fetch somewhere, so let’s just to it here 😄

Now to the child view:

This is pretty much a replica of our old ContentView.swift file. But the well trained eye 👁 will see that we don't call the fetch method! This is because the parent did it, so we don’t need to do it again 😄 But we could still access all the variables and methods of the class 😁

Thanks for reading 🙏

This is how it looks like for me ☺️ I got some different naming, and my getPhrase needs more params but the rest is the same 😄