SwiftUI Framework: First Impressions

Ahmad Fayyas
6 min readJun 9, 2019

--

This is not a tutorial of coding with SwiftUI, it is just an iOS developer’s first impression about how we’ll deal with the framework.

Hello, SwiftUI!

At June 03, 2019 Apple announced a brand new UI framework works across all Apple’s platforms, SwiftUI, evolutionary a curial change in the heart of iOS development.

For me as usual, when hearing of such a change: “Wuhoo! A new thing 😃… Oh really 😑”. I decided to briefly take a look at its documentation just checking the headlines and then directly getting my hands dirty by coding.

Side-bar tip: we (developers) usually love to go directly to the final results; At certain point, even copying-pasting a valid answer from StackOverflow without knowing what on earth is going on would be ok!.

At the very first look at the code snippets and the examples, I saw kind of non-usual coding style (at least for iOS development). After getting back couple of steps, I recognized that I read the intro too fast hence I skipped one of the most describing terms in the documentation, “declarative”.

Huh, “Declarative”?!

Wait a sec… Should I write such a code to create a table view?!

https://developer.apple.com/xcode/swiftui/

It may look cool and weird somehow. For me, the coolest part of it is: Less code! I don’t have to write so much boilerplate code (yes, I’m talking about you DataSources and Delegates). Nevertheless, this doesn’t look so familiar to create a table view, right? Actually, the mentioned coding style is an example of the declarative paradigm; The “usual” paradigm for us is called the Imperative paradigm.

Although There are many articles, blog posts, and questions/answers out there describing the difference between declarative and Imperative I’d remind that:

Declarative: What do I (code writer) want to do. Not interested in how you (service provider: compiler, framework, etc…) do it.

Imperative: How do I want to do it. I describe each step of how should it be done.

For clarification, let’s take a look to a pretty simple example to see what’s the main different between declarative and imperative:

Consider that we have an array of Ints and we want to get the elements with a value of larger than 100 of it, then we want to add “is larger than one hundred” after each one of them. Simple enough, how to resolve it? Like this:

let result = myArray.filter { $0 > 100 }.map { "\($0) is larger than one hundred" }

This is declarative coding! Think of it as you just told the compiler what is your expected result without specifying the details of it: “That’s what I want to get. How? I don’t care!”.

On the other hand, you can also resolve it as:

var result = [String]()for element in myArray {    if element > 100 {        let newElement = “\(element) is larger than one hundred”        result.append(newElement)    }}

and you’ll get the same result, you told the compiler exactly what are you aiming to do imperatively (loop through the array, check the value of each element, interpolate it in a string and add it to a new array).

Additionally, bare in mind that the first code snippet is better choice because of:

  • Readability: For most of the developers, the first code snippet is more readable (and I hope it is for you!).
  • Performance: The first code snippet avoid mutability (var result).

So you might already write declarative code without even recognizing it. Basically, that’s it.

Ok, let’s back to our table view thing. As I mentioned, I’d bid you wrote a lot of DataSource/Delegate methods implementation. We are describing the details for doing it, such as the number of the rows/sections, how the cell should look like (configuring the cell), and what should happen when tapping it

A quick reminder:

https://www.c-sharpcorner.com/blogs/tableview-prototypecell-in-swift

Probably, this is almost the simplest way to define how your table view should look like by following the “usual” Imperative paradigm.

In general, the main idea to keep in mind when aiming to create your table view declaratively is: Actually, we declare the list, we mention the model objects to be used for each row as well as the action for tapping the row; No setup, no cell configurations or callbacks! At least that’s the simplest form of it.

What does it mean to me as a developer?

I assume that some of the developers are keen to learn it, some of them are getting bored of dealing with new technology, and the rest of us like “ya whatever…”.

Regardless of which type you are, IMO I would recommend to learn it and deal with it. Apple focuses (and actually enforces at certain conditions) to keep the developers up to date with the latest provided technologies, if you have been working with iOS development for couple of years, you are already know exactly what I’m talking about. Maybe in future it will be the only way to setup our apps UI, or at least the current one we know may get deprecated.

Moreover, it’s worth to mention that SwiftUI could be more than just the “UI handler” part in our projects; Citing from SwiftUI - State and Data Flow documentation:

States and bindings connect views to your app’s underlying data model. When you declare a state, SwiftUI stores it for you and manages the state’s connections to its view. When the state updates, the view invalidates its appearance and updates itself. You can also connect animations to the state to animate how the view portrays the change.

Create bindings from members of your state to connect to individual views. Bindings offer two-way connections, so that onscreen controls can mutate the state. Bindings also have transactions to pass values between views.

Based on that, when reading “States and bindings” and “Bindings offer two-way connections” I guess that the first thing came to your mind is… YES! A new era with more than the standard MVC!

Does it mean that applying an alternative architectural patterns would be much easier and native without extra headache?!…

Finally

Declarative paradigm is already a popular one even if it’s new to you, for instance if you are a bit familiar with the JavaScript world, you’ll notice that many of its libraries and frameworks are following the declarative paradigm. My point here is to mention that the benefits of working with it will not only be reflected on your iOS development knowledge, it will definitely expand your general programing skills.

Without a doubt, there is a lot to learn for now -even if we have prior knowledge of dealing with “declarative” coding style-, let’s just prepare ourselves for it; Personally, I’m pretty sure that it will enormously make our life easier.

Furthermore

As expected, there is a wide potential for going into the SwiftUI world. I’d like point out to awesome-swiftui great repo, it comprehensively contains official/non-official websites, tutorials, libraries, sessions, and articles (including this one 😎) about SwiftUI.

Thanks for reading!

--

--

Ahmad Fayyas

Software Engineer. If I’m not in front of my pc coding or playing video games, you can find me hanging out with family and friends, or sleeping!