SwiftUI pitfalls

Scott
2 min readMay 4, 2021

--

Difficulties with @Binding:

  • False positives: Computed properties give you false positives, as they somehow do not provide the latest value.

Take the following struct:

Looks normal right?

If I pass this as a Binding, the formModel, employeeDestination, authViewModel will not update properly with the binding.

  • Restructuring issues: Without computed properties what options do we have?

Well we could do this and it will work but…

Its misleading. It will give you the freedom to assign a value to each of these variables, but it won’t actually assign a value to them. Not only that, I can’t say its very clean or swifty. Somewhat defeating the point of new Swift development.

Another alternative is this:

We are privately setting the variable values in the initializer and the property listener for the choosePathViewModel.

Now I’m getting these errors somewhere else in the code:

I hope you can see the difficulties I’m having. If you have a solution, please feel free to post it in the messages below.

  • Door #3 Protocols

I Could restructure my structs so that they don’t compose other structs as properties, but instead a “god struct” would have the primitive values of both, but would conform to a protocol that requires the values of the other struct that was composed. This would suck because it would require refactoring and I would lose logical structures and relationships. Also I wouldn’t be able to use default initializers, instead I’d have to take the properties from the smaller struct and assign them to the god struct’s properties.

--

--

No responses yet