Swift Regex Deep Dive
iOS MacOur introductory guide to Swift Regex. Learn regular expressions in Swift including RegexBuilder examples and strongly-typed captures.
SwiftUI, Apple’s new declarative programming framework, was introduced along with iOS 13 in September 2019. As a student rolling into 2021 you may be wondering if you should start your iOS development journey with SwiftUI or pick the tried and true UIKit? Spoiler alert: it depends.
UIKit provides a variety of objects which you can use to develop apps for iOS. These objects, such as UIView and its subclasses, allow for the display and interaction of content within your app. UIKit apps generally make use of the Model-View-Controller (MVC) design pattern.
UIKit has been the backbone of UI development on iOS for over a decade. It is a mature platform that sees use in just about every iOS application in existence. Since it is well established, there is an abundance of resources available in case you get stuck or have questions.
UIKit apps can be built in a few different ways:
.storyboard
and .xib
files, both of which describe layouts using XML.SwiftUI is Apple’s new declarative programming framework used to develop apps for iOS and Mac using Swift. The declarative approach is the key differentiator when comparing SwiftUI to UIKit. In UIKit you mediate the relationship between events and changes to presentation. With SwiftUI the need to mediate that relationship disappears since that is handled by the framework itself.
As far as building apps with SwiftUI, things are a bit more streamlined when compared to UIKit:
.storyboard
and .xib
files are not used in SwiftUI. The Swift code itself describes the layout rather than these opaque XML files.Here are some additional notes and caveats:
It is important to keep in mind that UIKit and SwiftUI are not mutually exclusive. It is possible to use UIKit code in a SwiftUI view and vice versa. Let’s take a look at some examples!
UIViewRepresentable is a protocol provided by the SwiftUI framework. Using this protocol it is possible to wrap an instance of a UIKit view so that it can be displayed with SwiftUI
Defining a UIKit view wrapper with UIViewRepresentable
:
@available(iOS 13, *) struct MyUIKitView: UIViewRepresentable { func makeUIView(context: Context) -> UITextView { // Creating, configuring, and returning the UIKit view here let view = UIView() view.backgroundColor = .blue return view } func updateUIView(_ uiView: UITextView, context: Context) { // Update the state of the view here. } }
Using the wrapped UIKit view in SwiftUI:
struct ContentView: View { var body: some View { VStack { Text("Hello from UIKit!") MyUIKitView() } } }
UIHostingController is a view controller which allows for the display of SwiftUI views into a UIKit view hierarchy. It can be used just like any other view controller in UIKit:
let hostingController = UIHostingController(rootView: Text("Hello from SwiftUI!")) present(hostingController, animated: true)
See Apple’s Documentation for more information.
Here at Big Nerd Ranch, we’ve had ample experience working with both UIKit and SwiftUI. We believe that UIKit is foundational to an iOS developer building apps on Apple platforms now and into the future. It is our recommendation that you start with that if you’re unsure of which to choose.
SwiftUI is a great choice assuming the minimum iOS version for the project is set to iOS 14. However, be aware that you may run into issues and limitations as you build your app. Having a foundation in UIKit would help you navigate these problems.
Learn UIKit. It’s a mature platform that can handle a wide range of user interfaces, from the simple to the complex. If you want to explore new frontiers, check out SwiftUI. Since they are not mutually exclusive, it’s even worth learning both!
Our introductory guide to Swift Regex. Learn regular expressions in Swift including RegexBuilder examples and strongly-typed captures.
The Combine framework in Swift is a powerful declarative API for the asynchronous processing of values over time. It takes full advantage of Swift...
SwiftUI has changed a great many things about how developers create applications for iOS, and not just in the way we lay out our...