As we all know, SwiftUI is a new framework that Apple launched with the recent release of XCode 11 with lots of ambition. SwiftUI gets better, less code, helps developers shorten development time with UI-related tasks, … So is it true that SwiftUI is better than the current UIKit, should we switch to using SwiftUI now? This article is my personal subjective assessment of SwiftUI.
First, you will have to consider the requirements of SwiftUI:
Mac Catalyst 13.0+
Hmm, that’s all the latest apple launches .With iOS13 release not so long ago, along with the frequent bugs on Apple’s early updates, expecting users to switch to iOS13 cannot be fast. So using SwiftUI will mean we will ignore a large number of existing users with iOS12 or iOS11.
Better app, Less code?
Next is Apple’s introduction to the
Better app, Less code is just advertising or is it really so good? Consider a simple example: Create a rounded and shaded ImageView. Ok, first if using UIKit, the most common and common way would be to circle on a view and cast a shadow on a view. The first is shading on a container view:
let outerView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
outerView.clipsToBounds = false
outerView.layer.shadowColor = UIColor.black.cgColor
outerView.layer.shadowOpacity = 1
outerView.layer.shadowOffset = CGSize.zero
outerView.layer.shadowRadius = 10
outerView.layer.shadowPath = UIBezierPath(roundedRect: outerView.bounds, cornerRadius: 10).cgPath
Then the rounded ImageView (same size as its Container) inside:
let myImageView = UIImageView(frame: outerView.bounds)
myImageView.image = .....
myImageView.clipsToBounds = true
myImageView.layer.cornerRadius = outerView.bounds.width / 2
So what is the transition to SwiftUI?
let image = isFromAssets ? Image(imageName) : ImageStore.shared.image(name: imageName)
let rightPadding: CGFloat = isFromAssets ? 60 : 0
.strokeBorder(Color.blue, lineWidth: 3))
.shadow(color: .gray, radius: 2, x: 2, y: 2)
Achievements on SwiftUI:
Above is the code for SwiftUI, it can be seen that it has been cut in half. And also much clearer than using UIKit. The reuse of SwiftUI is also a point worth mentioning. When programming with SwiftUI, the versions will not be surprised when its view structure is very different from UIKit, no longer drag and drop views to the view, storyboard behind a code, all are clearer than SwiftUI. . The view structure of SwiftUI is similar to the Web or React-native, now a screen will be analyzed, and written into small components, high reuse, … So in terms of less code, SwiftUi seems to have gotten better, but on the better side there are more aspects we can’t confirm right now.
After a period of experience, I feel, SwiftUi has outstanding advantages compared to using normal UIKit:
- No AutoLayout: SwiftUI replaces the storyboard with code, so reuse will be much simpler, will be less conflict when it’s all expressed in code. When developing with Storyboard, many people have a tragedy that storyboards on one hand and code changes a little, not to mention the class size or other genres.
- Live Preview: The best thing is the live preview, with the live preview will shorten “hours” watching the build bar on XCode. If before, just fix the offset or a little layout, we will have to run, build project, brew coffee and drink that cup to enjoy the waiting time for XCode build. With SwiftUI, just change the code, the Preview screen will change for us, will not take a lot of time to fix the stupid UI fix. But for now, I see that the Live Preview of XCode is currently not very complete when there is still lag. But maybe this will improve in newer versions. video
- Binding : If only SwiftUI and the above are not attractive to developers, but with a framework released at the same time is the
Combinethat we have always wanted is that Binding with Swift finally appears. already. Although not as complete and complete as RxSwift or RxCocoa, it has great potential to be able to replace it later. With Combine, binding between data and view is extremely simple and convenient.
With the above advantages, according to my opinion, the move to SwiftUI at the present time is probably not appropriate. Because iOS 13 is still new, and most users may not have updated it, but it is more concise, but the absence of familiar components in UIKit will make you surprised when shaking hands and View design, which leads to It is possible that the code will take longer than it is. Previously, Swift also had to go through a lot of new versions to be stable, SwiftUI and Combine are no exception, if you turn to learn now, it can be a bit difficult and the support from the community may not exist. much. And above all, this is my personal feeling when trying to work with SwiftUI. Through the article, I hope to provide you with basic information about SwiftUI