With SwiftUI receiving a lot of support from Apple and Swift community, more companies are starting to use it in production. However, older projects will have a lot of UIKit code and will want to refactor that to SwiftUI. Or they might want to adopt SwiftUI for newer parts of UI and reuse existing businesses logic. Some might even want to support iOS versions before iOS 13 and write SwiftUI counterpart views to their UIKit views so they can drop UIKit when switching to iOS 13 minimum App version.

Adopting SwiftUI is all great, but how do we refactor old UIKit code to SwiftUI without breaking any business logic?

In this article, I will attempt to answer that question.

Sample Project

I created a sample project in the form of a simple Counter App with two architectural implementations, using MVC(simple) and Clean Swift(scalable) design patterns. I would recommend downloading the project from here to follow along.

Firstly if you are doing any business logic, network request, mapping or any other computation inside your UIViewController class, you will have to decouple that into a separate class so that your UIViewController stays plain. The view controller should only display what the presentation layer tells it to display. And notify the business logic layer what actions the user has taken. You can achieve that decoupling with both MVC and Clean Swift example implementations.

UIKit implementation

In this article, I will walk you through Clean Swift implementation, so lets, first of all, see how Clean Swift works with a UIViewController module.

#mobile #swiftui #swift #ios #programming

Refactor UIKit Views to SwiftUI Using the Adapter Pattern
1.80 GEEK