Skip to main content

I joined a company about a year ago that decided before I joined to build (and also rebuild) UIs in SwiftUI. I’ve seen some posts often on this sub and other with folks asking if they should invest their time into learning UIKit or SwiftUI so I wanted to report just a few things that I found over the last year in using SwiftUI almost extensively.

Before joining this company, every iOS UI I ever built was done in UIKit. I was comfortable with it, I had full control, and I had no concept of the pleasures of working with a declarative framework.

Disclaimer: I am still learning SwiftUI and iOS development on a deeper lever and if anything I write here isn’t 100% accurate, let me know!

UIKit to SwiftUI

Coming from a background of only UIKit the transition to swift UI didn’t take too long. The blessing of working on a project was is already mature in SwiftUI is that similar views I needed to create already existed. So I could peak at how other screens were created an copy that.

We also had a Foundations team that built out almost all of the UI elements we’d need to accomplish a job so that helped. But in personal projects this wasn’t the case so things would need to be created from scratch if there weren’t APIs already provided by apple.

Development Speed

The time needed to develop screens in SwiftUI is a lot faster than in UIKit, point blank. For this reason alone I bought into the framework after a little bit of resistance. When I can get a POC of a new feature mocked up in less than a day I was thrilled. Changes to the UI could be done in SwiftUI in a fraction of the time that it would take in UIKit.

SwiftUI Helped Me Learn A Lot About Generics

SwiftUI is is a lesson in generics. All of our view models are protocol driven so including these inside of our SwiftUI screens had to look a little something like:

struct MyAwesomeView<ViewModel: AwesomeViewModelProtocol> {
  private let viewModel: ViewModel
  
  init(viewModel: ViewModel) {
    self.viewModel = StateObject(wrappedValue: viewModel)
  }
}
Swift

It was very cool to learn more about generics in this way. And I think this would be a big point for people wondering if they should learn… I’m now a better developer now because of what I learned about generics in SwiftUI.

SwiftUI and MVVM

A lot of the side projects and apps that I build prior to knowing SwiftUI were written using an MVC pattern. When it comes to SwiftUI, MVVM is the name of the game! Now I use MVVM in all of my personal projects regardless if I’m using SwiftUI or not. I finally understand the value of it.

At work we still use MVVM in our UIKit classes. We used RxSwift and RxCocoa for data binding. So now when we’re using SwiftUI we can build out view models using Apple’s Combine framework and they plug right into our SwiftUI views. For the screens we rebuilt that still used RxSwift in the Data and ViewModel layers we had to write an extension to RxSwift to convert these RxSwift classes to Combine classes. This works pretty good actually but it was a little confusing at first because I needed to learn RxSwift and Combine at the same time. A good resource I found for understanding the differences between Combine and RxSwift could be found here.

It’s Not Either/Or

SwiftUI can be used in UIKit applications using UIHostingController and we use these extensively. Conversely there is also the option of building the majority of a screen in SwiftUI and using UIKit for a very complex component with UIRepresentable. I will say UIRepresentable was a little bit scary to work with at first for me, but this was unfounded. It only requires two methods to be implemented and I found it to be dead simple after I dived in.

My recommendation now is to use both UIKit and SwiftUI in work and personal projects.

Introduction To Reactive Languages

Before using SwiftUI would you believe that I never had touched a reactive framework before? I kind of knew about RxSwift but only in passing. I had never looked into it because I had never worked on an enterprise application that needed it before.

SwiftUI is reactive by definition! So I needed to got on learned Combine as soon as I could. Combine has a little bit of a learning curve to it but after a while it makes sense.

I remember trying to get a publisher to work in a view model I was build. It looked like this:

awesomeThing.sink {
  // something here
}
Swift

It wasn’t working! I couldn’t figure it out. After a minute, I jumped on slack and asked one of the guys on my team what was up and he was like you need to assign the cancelable or store it in a class var. I had no idea what the hell he was talking about lol.

This is what I needed to do:

let cancelable = awesomeThing.sink {
  // something here
}
Swift

Supporting lower version of iOS

Things on iOS 15 and below get wonky. We were writing wrapper classes for different version of iOS and instantiating these views with #available statements. We’ve since moved from supporting iOS 14 to iOS 15 as a minimum version but we still have some of these workarounds scattered in our codebase but it’s getting a lot better. There is no greater joy than removing some of these structs from the codebase!

At this point I like to build personal apps in a way that allows me to use both UIKit and SwiftUI. If I’m building screens that are very complex and could really benefit from UIKit I want that option. Where as simple screens like settings screens and some list views I find are just easier to do in SwiftUI.

How to learn SwiftUI

There would be no better way to learn SwiftUI (or UIKit) then to just jump in! I did crack a book on SwiftUI when I was first getting started in and this taught me a lot of the nuances of the framework but it wasn’t a book there I learned the most. It was just building. What’s an app that you’ve been wanting to build? Jump into it and start building some screens with SwiftUI.

Overall I am a better developer now after learning SwiftUI and for sure there are companies that are looking for SwiftUI skills in their hiring. Building side projects can be done in a fraction of the time then if UIKit was the main UI framework. I now know reactive frameworks and concepts thanks to learning Combine and the bast things is, I can use both UIKit and SwiftUI in ios development, which is what I suggest because most application at companies will contain both and it’s good to know both!

David

Hi I'm David – I'm a creator, entrepreneur, and engineer. I add value to people to help them live a better life.

Leave a Reply

Get On David's Mailing List →
Close