Can we simplify let speedInt = try? await marker.viewModel.speed.values.first let speed: String = if let speedInt { "\(Int(speedInt)) km/h" } else { "-" }
Asked on 2025-11-13
1 search
To simplify the given Swift code snippet, you can use the new features introduced in Swift 6.2 that make handling optional values more concise. Here's a simplified version of your code:
let speedInt = try? await marker.viewModel.speed.values.first
let speed = speedInt.map { "\($0) km/h" } ?? "-"
This version uses the map function on the optional speedInt to transform it into a string if it has a value, and uses the nil-coalescing operator ?? to provide a default value of "-" if speedInt is nil.
For more information on concurrency and Swift, you might find the session Explore concurrency in SwiftUI helpful, as it covers concurrency optimizations and techniques in SwiftUI.

Explore concurrency in SwiftUI
Discover how SwiftUI leverages Swift concurrency to build safe and responsive apps. Explore how SwiftUI uses the main actor by default and offloads work to other actors. Learn how to interpret concurrency annotations and manage async tasks with SwiftUI’s event loop for smooth animations and UI updates. You’ll leave knowing how to avoid data races and write code fearlessly.

What’s new in Swift
Join us for an update on Swift. We’ll talk about workflow improvements that make you more productive, and new and modernized library APIs for fundamental programming tasks. We’ll show examples of Swift adoption throughout more layers of the software stack. Finally, we’ll explore new language features for both improving approachability of concurrency, and achieving peak performance when you need it.

Meet the next generation of CarPlay architecture
Dive into the architecture for the next generation of CarPlay. Learn how your vehicle system works with iPhone to create a single cohesive experience that showcases the best of your car and iPhone. Learn how UI is rendered and composited, and explore ways to configure and customize a special experience for each vehicle model. This session is intended for automakers and system developers interested in the next generation of CarPlay.
