SwiftUI
State driven declarative UI framework
At WWDC 2019 Apple announced its new framework called SwiftUI. The purpose of this new framework is to increase teams productivity and, considerably, reduce the amount of lines of code. Developers can now test the results of their work in real-time, so there’s no need to deploy the app on the simulator or on a physical device. As we are sure you are now an SwiftUI enthusiast you have to know it...yes! User interfaces can be built for any Apple device using just this framework. 🙌 🙌
Apple describes SwiftUI as “a modern way to declare user interfaces for any Apple platform”.
Before SwiftUI, developers could choose the desired framework to build the UI based on the target platform, for example, UIKit (for iOS and tvOS), AppKit (for macOS) and WatchKit (for watchOS). As developers, for creating apps for these platforms, we follow a programming paradigm known as imperative programming - a paradigm that uses statements that change a program's state.
Using this paradigm means constructing the views of our application so we can later change or mutate their state manually. Changes or mutations can be done by calling different methods (setters) or adding direct references to the views.
After having explained the imperative paradigm we are allowed to reveal it.
SwiftUI uses the programming paradigm known as declarative programming”
With the new Swift UI declarative way, data now updates the view automatically when you need it (ViewControllers are no longer needed, since they were needed to keep the view in sync with the data). This leaves the transitions between view states to be managed by the framework. SwiftUI knows when state changes, and updates views to match that actual state. So there is no need for developers to manually modify the state of the views using view setters.
“Declarative programming is a programming paradigm...a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.”
SwiftUI is a great framework for teams that are looking for a way of reusing components (views). If each view is constructed following Apple guidance, developers can end up having as many reusable views as needed. Furthermore, combining this benefit with a suitable architecture will bring us the possibility of building a modularized app.
Coding
To test its ease of use, we are going to show a simple list built using SwiftUI. This example was created after doing a master class of SwiftUI (created by Credo Academy).
Fruits List View
Fruit Row View
This portion of code allows us to display a list and fulfill all the interactions necessary for the desired navigation.
We built an entire screen just by adding 40 lines of codes. That’s really good!! 😎
Final Thoughts
SwiftUI is significantly newer, so it may be that there is no information regarding some of the problems that we face every day. That being said, we can affirm that in the coming years it will have a very strong impact on development of applications, since the advantages in code productivity and reusability are already being seen.