0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Swift のマクロ機能で作る、ディープリンク enum 自動生成ツール「URLPattern」

Posted at

概要

URLディープリンクは、現在ほとんどのサービスで広く使用されている基本的な技術です。しかし、Swift環境でディープリンクを実装するには、通常、URLパスの直接操作や正規表現の使用が必要になります。

// 従来の手法:手動でURLを処理
let paths = url.pathComponents

if paths.count == 2 && paths[1] == "home" {
  // ホームを処理
} else let match = try? url.path.firstMatch(of: /\/posts\/([^\/]+)$/) {
  // 投稿を処理
}

このような方法では、コードの可読性や拡張性が低下し、特に不正なパターンをコンパイル時に検出することができません。

URLPattern はこれらの問題を解決し、コンパイル時のURL検証と値のマッピングを提供します:

@URLPattern
enum DeepLink {
  @URLPath("/home")
  case home

  @URLPath("/posts/{postId}")
  case post(postId: String)

  @URLPath("/posts/{postId}/comments/{commentId}")
  case postComment(postId: String, commentId: String)
}

主な特徴

✅ URLパターンをコンパイル時に検証
🔍 URLパラメータと列挙型ケースの正しいマッピングを保証
🛠️ String、Int、Float、Double のパラメータ型に対応

GitHubでチェック:URLPattern

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?