Make your UIKit codebases look as clean as SwiftUI
The most noticeable thing about SwiftUI is that it looks incredible. It takes a relatively big chunk of code and squeezes it into something beautiful and straightforward so that even non-programmers can understand what’s going on.
But did you know that starting Swift 5.4, you can replicate that experience to anything you want?
You can build URL Requests, sophisticated data structures, and even a whole screen in UIKit the same way you do in SwiftUI.
In other words, you can implement DSL in your apps very easily.
If you haven’t heard the term “DSL”, it’s time to talk about it.
DSL stands for Domain-Specific Language.
But what does it mean?
When we look at a “regular” programming language, for example — Swift, we can see that theoretically, we can build anything we want.
It all relies on the framework we have underneath.
For example, we can build server-side applications, web, and Android if only we will have the proper framework.
Classes, structs, and Enums are not iOS-specific features.
On the other hand, a DSL is a language written to deal with a specific domain or a problem.
One good example is HTML.
HTML’s primary goal is to describe a document displayed on a web page.
HTML is excellent for that purpose and can easily be read by non-programmers.
Another example is XML or JSON, which are great for holding a general data structure.
Going back to Swift — SwiftUI is a DSL. Its primary purpose is to describe a dynamic screen, and it’s also built on top of Swift, which is a GPL (general-purpose language).
To create more DSL-style code, we need to use something called Result Builder, which is part of Swift Language and can help us create beautiful code.
Let’s try to understand how Result Builder works using an example, and we’ll go with an easy one — data structure.
Assuming we want to describe a data structure built from Team and Player.
Our data structure may look something like that: