How to build URLs in Swift

Building URLs safely and expressively is a common requirement in Swift applications—especially when dealing with REST APIs. In this post, we’ll explore four elegant approaches to construct URLs in Swift: manual string interpolation, Foundation’s URLComponents, a custom URL‐builder DSL, and an enum‐driven router. By the end, you’ll have a clear sense of trade‐offs and patterns you can adopt in your own codebase. Manual String Interpolation The simplest approach is to build the URL by concatenating strings: ...