Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a controller in SwiftUI?

I'm trying to get into SwiftUI right now, but struggeling with basic things. My struggle of the day: I'm wondering if there is something like a controller in SwiftUI? Where does logic not related to the UI go?

To give a concrete example:
I have an external framework. When the app starts, I fire up that framework (right now in the AppDelegate). It does some networky stuff and based on the result, I show one of two views. In one of them, the user has to input something, presses the OK button and I have to pass the input back to the Framework. How would I go about doing that?
I have no controller or coordinator and putting it in the View (through Singletons) seems wrong to me.


Sidenote: Interestingly, every single tutorial I find just omits stuff like this. They all focus on one more views, and a ViewModel, but never "zoom out" to show how an entire application (beyond two views linked with NavigationLink) work. If somebody can point me to one that tackles this, I would be very greatful.

like image 266
BlackWolf Avatar asked Oct 29 '19 09:10

BlackWolf


People also ask

What is SwiftUI controller?

In SwiftUI you use @State for local state, and function to mutate it. This is where you will find control. There are also other mechanisms to import external state such as @EnvironmentObject. And state changes would trigger view update, as React.

Is there a viewDidLoad in SwiftUI?

If you come from the UIKit world, you might wonder what the viewDidLoad() equivalent method in SwiftUI is. Too bad there is no direct replacement of viewDidLoad() in SwiftUI. The closest SwiftUI's methods we have are onAppear() and onDisappear() which is equivalents to UIKit's viewDidAppear() and viewDidDisappear() .

Is SwiftUI better than UIKit?

In general, most of the developers are of the view that SwiftUI is the fastest way to create in-app functionalities. It requires very less code and can help you achieve almost the same results in a much lesser time when compared to UIKit.


1 Answers

Sidenote: Interestingly, every single tutorial I find just omits stuff like this.

That is because MVVM doesn't put emphasis on control. It is built around having model-view binding. MVVM does not prevent you from writing bad control codes, and it requires binding mechanism not present in iOS until SwiftUI. It's by no means solution to everything as some Internet articles would like you to believe.

SwiftUI is influenced by MVVM? yes.

SwiftUI is based on MVVM? no. I'd argue it is more React-like.

Having no view controller does not mean you should do control in view model.

It just means you don't need an extra object to do control. Function itself is control.

In SwiftUI you use @State for local state, and function to mutate it. This is where you will find control.

There are also other mechanisms to import external state such as @EnvironmentObject.

And state changes would trigger view update, as React.

Some MVVM developer and their mother would create an object called view model with no binding support whatsoever; then spend a bunch of hours doing manual binding with Combine, and after several refactor commits later tell you MVVM is the second coming of Christ and best hope for clean architecture.

You don't have to. Obvious I'm opinionated, so take it with a grain of salt.

You only have to look for where and how SwiftUI does binding, and you will know view model is redundant. So no, SwiftUI is not based on MVVM. It can have model-view mapping without ever creating any view model. You don't need to learn MVVM to understand any of that.

Managing control in SwiftUI is easier if your control are functions. The removal of view controller means you don't have to worry about its life cycle and internal states. You can manage them through protocols and even introduce some functional programming paradigms.

If you take a closer look at SwiftUI design, you would notice the heavy usage of value type and removal of objects and their life cycle. Both are in direct opposition of MVVM paradigms.

Every MVVM developer uses reference type as view model; SwiftUI wants you to map value type to view. i.e.; struct Model: View. Swift is not Java, but you won't see anyone mention that.

like image 123
Jim lai Avatar answered Oct 26 '22 13:10

Jim lai