Create an ObservedObject without "@ObservedObject" (in protocol)

This may be tough because I think it breaks the usual way that people use Swift's Observed Object.

My case is:

I am creating a protocol, it constrains that the struct inheriting to it has to implement something like

@ObservedObject var obj: MyObject = MyObject()

to make everything work.

Now I want to eliminate this line from the struct and instead, handle it directly inside the protocol (using the extension to make this) because all struct inheriting to this protocol must have this line to make everything work, and plus, things inside the struct should not directly get access to it (I make all helper functions inside the extension of the protocol to make the calling experience as simple as possible and avoid unwanted value changing).

However, if I use the init(wrapperValue:) or init(initialValue:) of ObservedObject struct directly (which is what the Swift document does not suggest us to do), it is not "binding" at all. In other words, all the things are not updating dynamically.

We are not able to use any wrapper inside the protocol, and I want to have the same effect without using "@ObservedObject" in the struct inheriting to this protocol (which eliminates one useless line for all developers using this protocol and avoid struct itself directly getting access to this observed object).

If you do not understand why I want to do this, my goal is to make the coding experience simpler for every developer (I am building a Helper Framework to reduce the development cost). So this one-line elimination is really really important.

Terms of Service

Privacy Policy

Cookie Policy