Deep Dive in SwiftUI
Actually I just learn SwiftUI with Clean Architecture for around 10 days. I took online course to learn and improve my skill. Although I have experience more or less 7 years in iOS programming. My work not give me time to learn about SwiftUI.
So, why learn SwiftUI ?
SwiftUI introduced by Apple at the WWDC June 3, 2019. It was released as part of Xcode 11 and iOS 13, allowing developer to create UI using declarative Swift syntax.
t’s been almost 5 years since SwiftUI was introduced, and I haven’t studied it seriously, only knowing the surface level.
Along with SwiftUI, Apple also introduced Combine as part of reactive programming. This framework allows developers to handle asynchronous events by combining event-processing operators and linking them into chains. It is designed to work seamlessly with SwiftUI, providing powerful tools for handling data streams and reacting to data changes.
I used Combine more than half a year ago. Even though Combine is new, it is not much different from the concept of RxSwift, so it didn’t take long to understand Combine.
Why now ?
The longer I stay in the programming world, especially with iOS and Swift, I feel like I haven’t learned much in the last 2 to 3 years. Many things have been missed, as I have only focused on algorithms, architecture patterns, and design patterns, along with the ego of not using external libraries.
So now, I emptied my glass to fill with new lesson.
Target
- Supported version iOS 13
- Clean Architecture
- SwiftUI version 1.0
Tools
- Xcode 14.2
Steps
Create new Project
For creating SwiftUI project that support iOS 13, you need create project with Interface type Storyboard. Why we use Storyboard, because @main Property Wrapper and protocol App only exist in iOS 14.
Setup Project
Then we update the SceneDelegate to grab ContentView as root view and try to Run and Build.
Should developers still support iOS 13 when using SwiftUI, considering the advancements and capabilities introduced in SwiftUI 2.0 with iOS 14?
In fact, first time I create new Project with SwiftUI, I didn’t care about iOS version but when I try to make my own project to support older iOS version there is some change like
- There is no ProgressView in SwiftUI 1.0
- No NavigationStack (only support for iOS 16+, for iOS 13 use NavigationView)
- No NavigationPath (only support for iOS 16+)
- AsyncImage (only support for iOS 15+)
- Toolbar (only support for iOS 14+)
- Searchable (only support for iOS 15+)
Custom ProgressView
To use ProgressView in iOS 13 we can just create struct that Implement UIViewRepresentable like this :
struct ProgressView: UIViewRepresentable {
func makeUIView(context: Context) -> UIActivityIndicatorView {
let indicator = UIActivityIndicatorView(style: .medium)
indicator.hidesWhenStopped = true
indicator.startAnimating()
return indicator
}
func updateUIView(_ uiView: UIActivityIndicatorView, context: Context) {
}
}
struct ActivityIndicator: UIViewRepresentable {
@Binding var isAnimating: Bool
func makeUIView(context: Context) -> UIActivityIndicatorView {
let indicator = UIActivityIndicatorView(style: .medium)
indicator.hidesWhenStopped = true
return indicator
}
func updateUIView(_ uiView: UIActivityIndicatorView, context: Context) {
isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
}
}
AsyncImage
To simplify the project you can use external library or look for Online tutorial that handle load image with URL like
- SDWebImage
- SwiftUI CachedAsyncImage
So, during my 10-day journey learning SwiftUI and implementing Clean Code practices, things have been going well. I’ve managed to keep my code modular. However, one thing that’s still bothering me is how to separate navigation into different classes, specifically using the Coordinator Pattern.
Then choosing RxSwift, Combine or Async Await. So what we choose ?
First we compare RxSwift with Combine, for support older version of iOS I think you will consider RxSwift because Combine released with SwiftUI so Combine just support iOS 13+ but for performance Combine is better than RxSwift
Oke next when we choose Async Await, although Async Await is not Reactive Programming but the implementation is more cleaner that RxSwift and Combine but Async Await only appear in Swift 5.5 so if your project still need to support older Swift version, I think you need to consider to use two other. You also can combine Async Await with Combine.
Next
To enhance the code, I believe we should research Coordinators in SwiftUI. So next part hopefully I found solution about navigation.