SwiftUI, Is it ready for my next project?
“Write once and run everywhere” -WWDC
What is SwiftUI?
SwiftUI is a new and constructive method of creating interfaces across all Apple platforms. In fact, you will now be able to create user interfaces for all Apple devices with one set of tools and APIs.
SwiftUI was introduced at WWDC 2018 and has been supported by iOS since version 13. Unlike UIKit, which is commonly used in conjunction with storyboards, SwiftUI is completely software-based. However, SwiftUI syntax is very easy to understand, and a SwiftUI project can be quickly viewed using Automatic Preview.
SwiftUI was created for cross-platform use to build applications with less code than UIKit but with the same complexity.
Difference between SwiftU, Interface Builder and Storyboards
Whenever a new product appears on the market, it is natural to wonder about how these new tools will compare to the ones that it is supposed to replace. Specifically, we are talking about Interface Builder (IB) and Storyboards. Let’s take a close look to see how they are compared:
- Difficulties in further edit — Interface Builder has a lot of XML code, which is not easy to read or edit. storyboards have a habit of growing larger and larger over time, making it difficult to control any source changes. SwiftUI does not have this problem since editing is quite intuitive and straightforward.
- Lac of proper connection — Interface Builder doesn’t know much about our Swift code, and our Swift code doesn’t know much about Interface Builder. As a result, we end up with lots of unsafe functionality: we Ctrl-drag from IB into our code to connect something to an action, but if we then delete that action the code still compiles — IB doesn’t even show any warning if the code it intends to call no longer exists. The Storyboard has similar issues when creating view controllers and other functions.
- Integrating with Swift — Before Apple released Swift, all of its product was created based on Objective-C. However, Swift has a lot of advantages over Objective-C, such as value types, protocol extensions, and many other things. The new SwiftUI design was created to enjoy all benefits Swift has to offer, whereas IB and Storyboard are all designed around Objective-C.
Advantages of SwiftUI
SwiftUI is a completely new framework that allows you to design and develop user interfaces declaratively and with less code. Let’s learn some more advantages about SwiftUI.
- It offers Live Preview. This is a very convenient and progressive way to see the results of code execution in real time without having to build.
- SwiftUI makes it easier for designers and web developers to work with iOS Developer.
- You will write less code to achieve the desired result.
- WidgetKit extensions require SwiftUI and you may need to use it anyway.
- It’s an opportunity to learn a valuable skill and the toolkit of the future.
- Because the code and interface come together, we no longer deal with storyboards in screen designs.
- We no longer need the terms like @IBAction and @IBOutlet, and we don’t need to connect the elements on the interface with the code anymore.
- The codes we write for a single platform now work on other platforms of Apple.
- It’s easy to learn, and the code is simple and clean.
- It can be mixed with UIKit using UIHostingController.
- SwiftUI provides mechanisms for reactive programming enthusiasts with BindableObject, ObjectBinding, and the whole Combine framework.
- SwiftUI no longer needs Interface Builder. It was replaced by Canvas, an interactive interface editor. When writing code, the visual part in Canvas is automatically generated, and when you create visual presentation elements, they automatically appear in the code.
- Declarative syntax — This means that all you will have to do is tell the UI what you would like it to do. For example, you can write that you want several text fields and then enter all of the details, such as the font and alignment of each field.
Disadvantages of SwiftUI
Let’s talk about negative things, things that argue against the adoption of SwiftUI in your next project.
Limitations on iOS
If your project needs to run on older devices or support an operating system version of iOS 12 and lower, you can’t use SwiftUI.
Lack of Support
We must find a solution for a problem by searching the internet. Stack Overflow is a common source for results.
However, that’s only the truth for UIKit. SwiftUI is only two years out and not widely used by companies in production. Therein lies the danger, that you will be the first one who struggles with the problem at hand.
It will take more time because you are not that familiar with the toolkit. SwiftUI is very easy to get started with. However, you may have years of knowledge in UIKit, and this includes common bugs and gotchas, how to use the controls, delegates, testing, and shortcuts. You won’t move at as fast as what you are used to.
Limitations of Feature and Functionality
There are some features that are only available in UIKit so far. The last welcome additions to SwiftUI in iOS 14 include lazy stacks, a grid (like UIKit’s
CollectionView), along with many other features like adding a video player or map to your SwiftUI view.
However, there are still missing pieces to make SwiftUI able to completely replace UIKit’s
ViewControllers in your apps. There is no support for WebKit or for adding a text field to an alert.
It doesn’t allow you to examine the view hierarchy in Xcode Previews.
Higher Learning Curve
Other developers in your team may not know SwiftUI. That’s one of the biggest issues. You may think of an older developer who has stopped learning much and doesn’t like to try out new things.
Think also about future developers that will join your company. If you are certain that they will be checked for SwiftUI knowledge, this is not a showstopper. However, if that is not the case, think twice before hopping on to the SwiftUI train.
Lack of Standard
As a developer we all wants to practice a standard during development. In UIKit, we already have many great architectures, and we know how to structure our code. But still there is no best practice is formed for SwiftUI and there is no complete guide for testing of SwiftUI views.
Though SwiftUI is clear, easy to read, and convenient to use. But It’s still very young, so there isn’t much data on Stack Overflow. This means that you can’t get much help resolving complicated issues. SwiftUI has a completely different approach than UIKit to writing code.
Still it’s not fully functional. As you can see how many updates it takes to bring the Swift to stable state. So still it’s risky to start your your next project with SwiftUI.
And as there is no standard format or structure have to struggle lots to structure the code. Considering all above I think it’s not a good idea to start your next project with SwiftUI.