Table of contents
After Apple unveiled the new UI framework this June earlier 2019, all apple community became thrilled to see it in action and to try SwiftUI app development. Let’s take a closer look at a brand-new framework.
From imperative to declarative UI.
What is SwiftUI
SwiftUI is a new Swift-based advanced toolkit that is reshaping the way to design and build apps for every device in the Apple environment.
Helping to reduce development time and making the code more readable newly introduced Apple’s framework can build user interfaces, e.g. Custom Controls, Animations, Effects in an easy to understand and declarative way.
Let’s dive a bit deeper and review what SwiftUI is ready to offer.
SwiftUI with its declarative nature is changing the paradigm of UI development, by significantly reducing lines of code and runtime crashes.
This is probably one of the biggest benefits of SwiftUI.
Being widely used in cross-platform and Android development, the declarative approach itself isn’t a new concept. However, with SwiftUI it is entering the Apple environment for the first time ever.
Before iOS 13 developers were using UIKit with an imperative approach to coding. It means that a developer needed to handle the events, UI transition or maintain states by writing additional lines of code, every single time an event should be reflected in the UI. With an app getting more and more complicated, a developer cared an extra burden to code an app. This burden could be, of course, offloaded by using extra, non-native tools, like RxSwift and RxCocoa. With the new framework from Apple, it will be no longer required and everything will be covered by means of native tools.
The declarative approach allows Apple developers to announce all states for the view at once. Thus, the additional code to move between states is no longer required. Instead, SwiftUI is completing this task on programmers’ behalf according to the declared rules at the very beginning. The main task left for programmers is to form the display model and to update it, SwiftUI will update UI automatically.
Before SwiftUI when creating UI in Storyboard you had a rather big XML file with complicated code, that was difficult to read. This XML file was later compiled to machine code.
With SwiftUI you can create a code on Swift straight away without any extra files, increasing the compilation time.
Being a part of the iOS SDK, SwiftUI works perfectly with native visual builders, such as Xcode. In its new release Xcode supports the two-way design, meaning that all manipulations in the Canvas, are reflected in the code updates, and vice versa. The best part – it happens automatically.
This feature gives developers additional freedom to choose between either working in a visual manner or coding directly. Whatever approach is chosen, any changes are reflected in one source code.
Native apps across all Apple platforms
As SwiftUI is using the same API, that is included into iOS, iPadOS, macOS, watchOS and tvOS, developers can build apps for different Apple platforms in one tool, instead of using two independent frameworks AppKit and UIKit for Mac and iOS development respectively.
Interactive Previews with Xcode
Designing, building, and testing are now possible on the code writing stage.
The Swift framework enables developers to see code modifications in the visual design tool. Being able to see changes in the runtime, developers can promptly check how the app is assembled, test and, if necessary, refine the code. Interactive previews in the runtime unleash more fun and efficient coding and enable better collaboration between developers and designers.
On top of that, previews can run on any connected Apple devices, e.g. iPhones, iPads, iPods, Apple Watches and Apple TVs. It gives the developers instant feedback on how the app, that is only being built, will respond on different platforms. Xcode is also availing the opportunity to swap edited code in the live app.
Supplemented with the bug detection tool embedded in Swift, it leads to better code in the end-product.
Drag and Drop
In SwiftUI you can arrange UI components inside the user interface, by dragging various controls from your library and drop them on the design Canvas or directly on the code. It is also possible to rearrange controls, open an inspector to change the design, e.g. font, color, alignment, etc.
Inspector can also be used to discover modifiers for each control. What’s more, it can be opened in both Canvas and code.
SwiftUI is a brand-new, freshly released framework that hasn’t been massively used. Thus, at the very first stages of massive implementation different bugs and negative issues may arise. As the community is not yet formed, there is no big knowledge base besides Apple tutorials. The first versions are in beta, meaning a lot of work needs to be done before SwiftUI becomes the primary choice for all Apple development.
At the moment, we can see there is no quick way to test the design on different screen sizes as in storyboard. Also, the syntaxes are new to most developers even though you can set the properties in the corresponding property inspector.
To work with SwiftUI you need to have Apple’s new OSs: macOS Catalina 10.15, iOS 13, iPadOS 13, tvOS 13, and watchOS 6. Until September, the official release there won’t be any apps coded with SwiftUI. Adoption of the framework will take time, as developers need to support operating systems a year or two back.
SwiftUI Impact on App Development
A declarative approach to programming can decrease an app building time significantly. However, it won’t happen overnight. First apps will take even more time to build as the SwiftUI app developers will need to get acquainted with the new tool and to feel it.
SwiftUI was created to stay for the long run. It’s an advanced tool, that brought declarative programming to Apple development, altering the basics of Apple programming.
Building with SwiftUI means:
- using declarative syntax
- building with less code
- reducing code complexity
- being backed by native Apple support
- inheriting free features and improvements created by Apple
In our opinion, stable applications built with Apple’s new framework may appear in 2 -2.5 years, when all first bugs will be tested and community support will increase.
SwiftUI is the future of Apple development, and this future is yet to come. At this stage, we would recommend all SwiftUI app developers to discover this tool and create pet projects using Swift framework to be ready to take full advantage of this framework once stable versions are available.