SwiftUI was released at the end of 2019. Just like every year, Apple announced multiple updates for developers. After the event, I promised myself I was going to get ahead of time and learn it right away (yeah just like iOS developers version of New Year’s Eve resolutions), truth is… I didn’t.
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 eventaully be. As the title states, this comes from my first glance into SwiftUI.
Pre-requisites
The first thing I noticed and which sidetracked me in the beginning, is that you need macOS Catalina and Xcode 11 or above. 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
Sometimes I feel confused at why Apple keeps changing things so fast, while their devices remain the same (almost) every year. The reasoning behind the release of SwiftUI is very solid and it covers several points:
1. SwiftUI unifies User Interface frameworks
SwiftUI is the next version of user interfaces for Apple Development. It is different from the previous UI development methods, XIBs, and Storyboards, because it unifies 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 its own post. Personally, I never cared for the differences among them as most clients always required apps for iOS and TVOS.
2. It has dynamic previews
I think an image explains this better than words:
Some important things to note in the above picture:
a) This preview pauses often (for example when you run a build). This is a bit annoying because 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 with the addition of new panels it is getting harder.
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. Making a closer analogy I’ve seen lots of projects lately that incorporate ReactiveX, with RXSwift and RXCocoa following these ideas.
Most mobile developers are familiar with the state of the UI, even on a subconscious level. Meaning that you are always aware of 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 User Interface binding was introduced for the Android platform, which falls 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. According to my perspective, one of the biggest downsides of developing in this platform is Interface Builder. Although it is a great tool, and it has made huge progress since it was first introduced, I highly doubt there is a developer who had not found himself 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 ‘Interface Builder’ views.
TL;DR
SwiftUI seems like a promising tool for iOS developers. It unifies all the UI frameworks of Apple, which will allow us to develop cross-platform apps more easily. This new tool is also 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
Lastly, I’m mentioning some useful resources that got my attention while writing this topic:
GitHub: About-SwiftUI
This repo was released right after the announcement of SwiftUI and it has collected a huge amount of useful resources, take a look at it for a wide variety of helpful items:
Link: https://github.com/Juanpe/About-SwiftUI
Apple Documentation
Thanks for reading! See you soon!