A Swift freestanding expression macro that validates URLs at compile time. Never crash from a malformed URL string again — #URL("...") guarantees the URL is valid before your code even runs.
Swift Macros (introduced in Swift 5.9) enable compile-time code generation and validation. URLMacro leverages this to move URL validation from runtime to compile time, catching invalid URLs as build errors rather than runtime crashes.
Add URLMacro to your Package.swift:
dependencies: [
.package(url: "https://github.com/maniramezan/URLMacro", from: "1.0.0")
]Then add "URLMacro" to your target's dependencies:
.target(
name: "MyApp",
dependencies: ["URLMacro"]
)import URLMacro
// Compile-time validated URL — guaranteed to be valid
let apple = #URL("https://www.apple.com")
let api = #URL("https://api.example.com/v1/users?page=1#top")Invalid URLs produce compile-time errors:
let bad = #URL("not a url")
// error: #URL requires a valid URL literal
let interpolated = #URL("https://\(host).com")
// error: #URL does not support string interpolationMIT — see LICENSE for details.