A Swift macro that provides compile-time URL validation with #URL("…") syntax.
✅ Compile-time URL validation - catch invalid URLs at build time, not runtime
✅ Zero runtime overhead - expands to simple URL(string:)! calls
✅ Clear error messages - descriptive compiler errors for invalid URLs
✅ Cross-platform - works on iOS, macOS, tvOS, watchOS
import URLMacro
// ✅ Valid URLs - compile successfully
let apple = #URL("https://www.apple.com")
let github = #URL("https://github.com/user/repo")
let file = #URL("file:///tmp/document.pdf")
// ❌ Invalid URLs - compile-time errors
let invalid = #URL("not-a-url") // Error: Invalid URL
let empty = #URL("") // Error: URL must not be empty
let interpolated = #URL("https://\(host)") // Error: String interpolation not allowed- File → Add Package Dependencies
- Enter:
https://github.com/alex-npmn/URLMacro.git - Add
URLMacroto your target
dependencies: [
.package(url: "https://github.com/alex-npmn/URLMacro.git", from: "1.0.1")
],
targets: [
.target(name: "YourApp", dependencies: [
.product(name: "URLMacro", package: "URLMacro")
])
]- Swift 5.9+ (Xcode 15+)
- iOS 15.0+ / macOS 12.0+ / tvOS 15.0+ / watchOS 8.0+
The #URL macro validates URLs at compile time and expands to regular Foundation.URL calls:
// This code:
let url = #URL("https://example.com")
// Becomes:
let url = Foundation.URL(string: "https://example.com")!The macro ensures:
- ✅ URL string is not empty
- ✅ URL has a valid scheme (http, https, file, etc.)
- ✅ URL has a valid host (except for file:// URLs)
- ✅ Only string literals are allowed (no interpolation)
This package supports SwiftSyntax 509-601 (Swift 5.9-6.1) to maximize compatibility with other packages and minimize dependency conflicts.
URLMacro is available under the MIT license. See the LICENSE file for more info.