Currently, Objective C and Swift are the languages, we’ve been using for developing iOS applications. Now, Apple comes with the new declarative approach called SwiftUI, which is an innovative, exceptionally simple way to build user interfaces across all Apple platforms with the power of Swift.
In this, we can easily write code and check the preview of UI instantly. Likewise, we can make changes in UI and it will reflect in code, as well.
In the existing approach, Swift/Objective C code statements vary for each platform. i.e, iOS application code is varied from the Mac application code. But declarative programming makes it simpler to adopt the code for all of Apple’s platforms.
All views are extremely lightweight so there’ll be more performance improvements as compared to the existing approach for creating views — because it avoids the use of heavily used subclasses and memory allocations.
Also, it composites all subviews in a single entity during the layout of a screen so each view becomes distinct and thus, makes way for observing state changes and the ability to render only if any state changes.
- Apple’s existing UIKit’s core design philosophy is the inheritance
- SwiftUI’s composition
The major advantage is that it can translate views to visual interface elements according to platforms like iOS, macOS, watchOS, tvOS.
For example, a Toggle view can be visualized differently on each Apple’s platform like below.
Here you can see the toggle button size and layout differs from iOS/watchOS, macOS and tvOS. This is based on each platform’s controls and in accordance with its screen size.
Likewise, it will properly adapt to changes in colors, padding, size alignment, etc. based on the platform.
For example, a button might be small in an iPhone and the small code will change the button size according to the iPad screen.
- We can visualize UI for changes without the need to recompile the whole application
- Reduces screen designing time
- Enables better integration between Developers and Designers
- Improves the speed of development
- Views can adopt properties according to different Apple’s platforms (iOS, macOS, watchOS, tvOS)
- It provides Automatic support for Dynamic Type, Dark mode, localization. So, no need to write any code specifically for Dark mode
- It also provides an option to use existing architecture UIKit by using UIHostingController
- SwitUI replaces storyboards and auto layout, so merging code becomes easier when compared to fixing storyboard conflicts