Back in June, we shared our Apple WWDC19 highlights – one of which was the introduction of SwiftUI. After spending the last few months exploring this new Apple UI framework in-depth, I came up with the following insights into getting started with SwiftUI.
SwiftUI is an API that lets us write UI code in a declarative way. This means writing your code in such a way that it describes what you want to do and not how you want to do it. SwiftUI provides views, controls, and layout structures for declaring your app’s user interface.
Declarative programming is the contrary of imperative programming which is what the iOS community are used to.
In an imperative language, we use a sequence of statements to get an outcome. Let’s use a login screen as an example, where we want to show a username field, a password field and login button.
In this screen, for example, we can have three states:
So, we basically have to write code to handle all different scenarios.
In contrast, declarative code will handle the state all at once. SwiftUI can move between user interface layouts when the state changes. We already told it what to show based on whether the user was logged in or out so, when we change the authentication state, SwiftUI can update the UI on our behalf.
Let’s look at how we can handle the different states with Swift UI. For the purposes of this demonstration, let’s handle the second state.
We declare a State property of a boolean type and set its default value to false:
When the State’s value becomes true that means that the authentication process failed. In this case, we want to show a Text object with an error message. We, therefore, write the following:
That’s what it means by declarative: we aren’t making SwiftUI components manually show and hide. We’re just informing it of all the rules we want it to follow and trust in SwiftUI to enforce them.
No. Many parts of SwiftUI directly build on top of existing UIKit components. Apple keeps adding new functionalities to UIKit so there’s no need to worry just yet!
Furthermore, SwiftUI runs on iOS 13, macOS 10.15, tvOS 13, and watchOS 6, and any future later versions of those platforms. This means if you work on an app that must support the current version and one or two before that – it will be a year or two before you can even think of moving to SwiftUI.
You need macOS Catalina and Xcode 11 Beta in order to have SwiftUI render the canvas properly.
In Xcode-Beta just go ahead as usual and create a new project and select ‘Single View App’:
Make sure SwiftUI is checked before you finally create your project:
The Single View App template includes the following files:
SceneDelegate.swift is responsible for managing the way your app is shown. Go ahead and open SceneDelegate.swift and you’ll see code like this:
That code above creates a new ContentView instance and places it inside a window so it’s visible onscreen.
Open ContentView.swift and let’s look at some actual SwiftUI code. You should see code like this:
Notice ContentView is a struct. Yay! Bye-bye mutability! With SwiftUI, we get to benefit from all of the immutability and simplicity of value types for our user interface!
ContentView conforms to the View protocol. Everything you want to show in SwiftUI needs to conform to View and you need to have a property called body that returns a view. That’s it!
Inside the body property, there’s Text(“Hello world”). This creates a label of the text, ‘Hello World’.
Finally, there’s the ContentView_Previews.
This is specifically there to show view previews inside Xcode. This is why you’ll see it inside #if DEBUG and #endif lines – this code is only built into the finished product when our app runs in a debug environment.
And those are the very basics when it comes to getting started with SwiftUI. In a future post, I’ll be exploring the components that we introduced in this very post.
Have you experimented with SwiftUI yet? If so, what are your thoughts? Tweet us and we’ll be sure to retweet the responses!
We Are Mobile First is a digital product agency based in Barcelona helping to transform businesses in a mobile-first world. Follow us on Twitter, LinkedIn and Medium to be notified of our future posts and stay up-to-date with our company news.
(image credit: Apple Developer)