You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Swift Macro that helps mapping URLs to Enum cases.
5
+
6
+
## Overview
7
+
8
+
URL deep linking is a fundamental technology widely used in most services today. However, in Swift environments, implementing deep linking typically requires direct URL path manipulation or regex usage:
9
+
10
+
```swift
11
+
// Traditional approach with manual URL handling
12
+
let paths = url.pathComponents
13
+
14
+
if paths.count==2&& paths[1] =="home" {
15
+
// Handle home
16
+
} elselet match =try? url.path.firstMatch(of: /\/posts\/([^\/]+)$/) {
17
+
// Handle posts
18
+
}
19
+
```
20
+
21
+
This approach reduces code readability and scalability, and importantly, cannot validate incorrect patterns at compile-time.
22
+
23
+
URLPattern solves these issues by providing compile-time URL validation and value mapping:
2. Add `@URLPath` macro to enum cases with the desired URL pattern.
66
+
67
+
3. Use path values with `{associated_value_name}` syntax to map URL components to associated value names. If mapping code is duplicated, the topmost enum case takes precedence.
0 commit comments