Understanding SwiftUI: New Swift 5.1 Syntax, Opaque Result Types, Property Delegates, and Function Builders
This article explains SwiftUI's declarative UI approach by introducing Swift 5.1 language features such as Opaque Result Types, Property Delegates, and Function Builders, illustrating their implementation with code examples, and discussing component composition, data flow, preview capabilities, and future prospects for mobile development.
Background
Apple introduced SwiftUI at WWDC 2019 as a declarative framework for building UI across watchOS, tvOS, macOS, and iOS, promising "Better apps. Less code." To experiment with SwiftUI you need Xcode 11 beta and macOS Mojave or later.
SwiftUI Features
The article focuses on three core language features introduced in Swift 5.1 that power SwiftUI.
2.1 Opaque Result Type
SwiftUI uses some View to hide the concrete view type returned by a body closure. This enables a flexible API where the compiler guarantees the returned type conforms to View without exposing the exact type.
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}Without an opaque result type, the body would need to specify the exact view type, limiting composability.
2.2 PropertyDelegate (Property Wrappers)
SwiftUI leverages property wrappers such as @State to automatically synchronize UI with data changes. The wrapper implements DynamicViewProperty and BindingConvertible , establishing a binding between the property and the view.
@propertyDelegate public struct State
: DynamicViewProperty, BindingConvertible {
public init(initialValue value: Value)
public var value: Value { get nonmutating set }
public var binding: Binding
{ get }
}When a @State property changes, SwiftUI detects the mutation, triggers a view re‑render, and updates only the affected parts.
2.3 FunctionBuilder
Function builders (e.g., ViewBuilder ) allow SwiftUI to treat a closure containing UI components as a DSL. The compiler rewrites the closure into a series of buildBlock calls that assemble a TupleView or other composite view.
struct RowCell: View {
let image: UIImage
let title: String
let tip: String
var body: some View {
HStack {
Image(uiImage: image)
Text(title)
Text(tip)
}
}
}If the number of child views exceeds the builder’s limit, developers can combine them using TupleView .
Components
SwiftUI encourages component‑based development. Views are reusable, declarative, and can be composed across all Apple platforms. Example of a button component:
struct ContentView: View {
var body: some View {
Button(action: { /* did tap */ }) {
Text("Click me")
}
.foregroundColor(Color.white)
.cornerRadius(5)
.padding(20)
.background(Color.blue)
}
}Live Preview in Xcode
SwiftUI previews provide a hot‑reloading experience similar to React Native or Flutter. Xcode statically analyses code (via SwiftSyntax) to find types conforming to PreviewProvider and renders them in real time on macOS 10.15 or later.
Outlook
SwiftUI introduces a new Swift coding style and promises cross‑platform UI composition, but its adoption is limited by the requirement of iOS 13+ and macOS 10.15+. Nevertheless, it paves the way for future unified front‑end frameworks.
Ctrip Technology
Official Ctrip Technology account, sharing and discussing growth.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.