SwiftUI is Apple's brand new framework for building user interfaces for iOS, tvOS, macOS, and watchOS. Apple introduced SwiftUI in 2019 and the framework has been evolving at a rapid pace ever since. Unlike UIKit and AppKit, SwiftUI is a cross-platform framework. With SwiftUI, Apple offers developers a solution for rapidly building applications. In this series, you learn everything you need to build applications using SwiftUI.
What Is SwiftUI?
SwiftUI is brand new, but it looks and feels familiar. Even though the concepts underlying SwiftUI are very different compared to UIKit and AppKit, Apple didn't reinvent the wheel. The framework offers everything you need to build user interfaces, such as lists, stacks, buttons, pickers, and many more components you are already familiar with through UIKit and AppKit. It also provides the tools you need to create custom views, add animations, and integrate gestures.
Support for core technologies, such as accessibility and adaptive layouts, is built into the framework. Like I said, the idea underlying SwiftUI is providing the tools you need to build user interfaces easily and rapidly so you can focus on the features that make your application stand out. That is Apple's goal with SwiftUI.
UIKit and AppKit
Even though SwiftUI is very different from UIKit and AppKit, know that SwiftUI relies on these frameworks to build your application's user interface. Under the hood, your application continues to use the components UIKit and AppKit provide, including UIView
and NSView
. The key difference with UIKit and AppKit is that SwiftUI defines the user interface declaratively, not imperatively. What does that mean?
Using UIKit and AppKit, you create views to build the view hierarchy of your application's user interface. That is not how SwiftUI works. SwiftUI provides developers with an API to declare or describe what the user interface should look like. SwiftUI inspects the declaration or description of the user interface and converts it to your application's user interface. SwiftUI does the heavy lifting for you. You learn how that works in this series.
Managing State
One of the most challenging aspects of user interface development is synchronizing the application's state and its user interface. Every time the application's state changes, the user interface needs to update to reflect the change. Using UIKit and AppKit, synchronizing the application's state and its user interface is mostly a manual task. Frameworks such as Combine make this easier, but it is still mostly a manual task that is a common source of bugs.
SwiftUI makes this a thing of the past. A view is no longer the result of a sequence of events. A view is a function of state. In other words, the user interface is a derivative of the application's state. If the application's state changes, the user interface automatically updates, eliminating a whole category of bugs. How that works becomes clear later in this series.
Xcode Support
SwiftUI is more than a framework. It is integrated into Xcode, enabling developers to build user interfaces much faster than ever before. You can edit the user interface of your application in code or in a visual editor that automatically reflects what your code translates to.
Xcode provides the ability to preview the user interface you are building. This is possible by compiling and running the code you write and displaying it in a preview. Previews can be made interactive, eliminating the need to run your application in a simulator or on a device. This is more than a nice to have feature. It drastically speeds up user interface development. We take a closer look at previews in this series and the possibilities they offer developers.
Cross-Platform Framework
It has never been easier to create an application that runs on every Apple platform, including macOS. Building applications for macOS used to be intimidating. It no longer is thanks to SwiftUI. The building blocks you use to build your application's user interface are the same, regardless of the platform you are targeting. SwiftUI takes care of making sure the user interface looks and feels great on every platform.
What's Next?
Even though SwiftUI relies on UIKit and AppKit, it takes a very different approach to building user interfaces. Its declarative syntax simplifies user interface development and its integration with Xcode makes building and debugging user interfaces easier and faster than ever before. SwiftUI continues to evolve at a rapid pace, opening up new possibilities with every major release.