Reading Time: 4 minutes

It has been a month and a half since SwiftUI came out, and like every year, when Apple announces it’s multiple (overwhelming) updates for developers like me, I promised myself I was going to get ahead of time and learn it right away (yeah just like iOS developers version of New Years Eve resolutions), truth is… I didn’t. So today I took my first dive into it and wanted to make it easier for anyone who still hasn’t had the time to review it, because let’s be honest, at the moment, it is not required in our every day, but it will someday be. As the title states, this comes from my first glance into SwiftUI.

Pre-requisites

The first thing I found out (and which sidetracked me in the beginning), is that you need macOS Catalina and Xcode 11. Back then, Catalina was still in Beta and I didn’t want to deal with those bugs in my projects. But now it is available and you only have to have a bit of patience to install both things if you don’t have them already. If you have that, you are good to go.

What is SwiftUI

I don’t know about you, but in my opinion, sometimes it puzzles me why Apple keeps changing things, while their devices remain the same (almost) every year. This answer covers several points:

1. SwiftUI unifies user interface frameworks

SwiftUI is the next version of user interfaces for Apple Development. What is different from the previous methods (XIB and Storyboards) is that it provides unification among the different UI Frameworks: UIKit (iOS and TVOS), AppKit (MacOS), and WatchKit (WatchOS), explaining the differences among these frameworks is a topic in itself that would require a post in itself, personally, I never cared for the differences among them as most clients always require apps for iOS and TVOS.

2. It has dynamic previews

I think an image explains this better than words:

Dynamic preview (right), SwiftUI Code (left)

Important things to note in the above picture:
a) This preview pauses often (for example when you run a build) and you can’t interact with it while it’s paused.
b) You can cmd+click on objects/views to get a menu with options
c) Code can seem very confusing at first, but if you have experience with Swift you will start understanding it soon enough.
d) You need a big screen! I know most developers work on separate monitors, but I personally like working only on the laptop’s screen and it is harder every time.

3. It incorporates ‘state’ control in the UI

State-oriented apps are very common nowadays. In the Javascript world (I worked some time with React Native), you listen a lot about Functional Programming and this ‘state/stateless’ components. I won’t extend in the topic as it is out of the scope of this article but, making a closer analogy I’ve seen lots of projects lately that incorporate ReactiveX, with RXSwift and RXCocoa following these ideas. To be honest, I don’t think this is really a new concept, most mobile developers are familiar with the state of the UI, what I mean through this is that you are always aware whether a button was tapped, Wifi turned on or off, the camera is available, and a ton of similar examples that represent this same state concept. During Google I/O 2019, the concept of data binding and UI binding was introduced for the Android platform, this goes under the same topic of state control.
In summary, you can bind a view to the corresponding state (this is a bi-lateral connection) and whenever the state gets updated, the view renders again, and vice-versa, if the view updates, it can change the corresponding state.

4. We can finally edit the code of our views

I think this was much needed in Apple’s development platforms, one of the biggest downsides of developing in this platform, in my opinion, was the strongly bonded it was with Interface Builder. Although it is a great tool, and it has made great progress since when it was first introduced, I highly doubt there is a developer who had not found himself/herself annoyed with a merge conflict due to a small change in IB. Now with SwiftUI, we know which part of the code refers to the interface, and I hope this will make our lives easier. Now you don’t have to choose between ‘code’ views vs ‘IB’ views.

TL;DR

SwiftUI seems like a promising tool for iOS developers. It unifies UI frameworks of Apple, which will allow us to develop cross-platform apps more easily. This new tool also is part of the ‘state’ driven development area, which is a trending topic within developers. You can preview your app UI in a very similar way as you did previously with XIBs and Storyboards, but with the advantage that you can see and edit the code, it is generating.

Resources

For this topic, you have the usual resources, and I may not list them all, but I will add some that caught my attention:

GitHub: About-SwiftUI

This repo was released right after the announcement of SwiftUI and it has collected a huge amount of the information out there on SwiftUI, take a look at it if you want a wide variety of options.

Link: https://github.com/Juanpe/About-SwiftUI

Apple Documentation

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

If you liked the content on this post, please follow me at the bottom of the page. You can also buy me a coffee to support me!

Until next time!

Evana Margain Puig

If you need any services regarding mobile apps please come to my webpage, evisoft.mx or drop me a line contacto@evisoft.mx.

(Visited 12 times, 1 visits today)

Leave a Reply