Quick Tip Icon
Quick Tip

Nil coalescing for optionals in debug prints

To make it easier to use Optionals with string interpolation in debug prints in Swift, we can define our own operator that lets us use a String as default value independent of the Optional type.

infix operator ???: NilCoalescingPrecedence

func ???<T>(
    optionalValue: T?,
    defaultValue: @autoclosure () -> String
) -> String {
    optionalValue
        .map { String(describing: $0) } ?? defaultValue()
}

var url: URL?

// assign a URL some time later
url = URL(string: "https://example.com")

print("The URL is \(url ??? "nil")")
Integrating SwiftUI into UIKit Apps by Natalia Panferova book coverIntegrating SwiftUI into UIKit Apps by Natalia Panferova book cover

Check out our book!

Integrating SwiftUI into UIKit Apps

Integrating SwiftUI intoUIKit Apps

UPDATED FOR iOS 17!

A detailed guide on gradually adopting SwiftUI in UIKit projects.

  • Discover various ways to add SwiftUI views to existing UIKit projects
  • Use Xcode previews when designing and building UI
  • Update your UIKit apps with new features such as Swift Charts and Lock Screen widgets
  • Migrate larger parts of your apps to SwiftUI while reusing views and controllers built in UIKit