减少编写自定义 URL 匹配和解析逻辑的时间。使用单个字符串定义您支持的任何 URL 格式,让 Corridor 完成繁重的工作。
假设您要处理像这样的用户个人资料通用链接:https://example.com/user/15127128
。
(1) 创建一个路由 -- 一个 Codable 结构体,其中包含您要从 URL 中提取的所有值。一旦 URL 匹配,userId 将具有 15127128
的值
struct ViewUserProfile: CorridorRoute {
let userId: Int
}
(2) 注册格式:"/user/:userId{int}"
router.register("/user/:userId{int}", { try corridorResponse($0, ViewUserProfile.self) })
现在,让我们看看如何使用它
(3) 使用 URL https://example.com/user/15127128
调用 attemptMatch(_:URL)
以获取匹配的响应
let result = router.attemptMatch(url)
(4) 切换到结果的 route
let route = result.route
switch route {
case let route as ViewUserProfile:
print(route.userId)
default:
print("url not matched")
}
15127128
瞧!
查看使用 Corridor
的示例路由器,位于 Example
文件夹中
一个符合 CorridorRoute
的结构体。它的变量对应于 URL 中包含的值。当 URL 匹配时创建。
路由器有助于将 URL 匹配到路由的过程
let router = CorridorRouter()
让我们注册一种 URL 类型
router.register("/user/:userId{int}", { try corridorResponse($0, ViewUserProfile.self) })
ViewUserProfile 是我们在第一个示例中定义的结构体
"/user/:userId{int}"
是 URL 格式。它匹配以 /user
开头并在其后具有整数值的 URL。
{ try corridorResponse($0, ViewUserProfile.self) }
是一个代码块,如果 URL 匹配,则填充路由 ViewUserProfile
。
URL 格式分为路径和查询
URL 格式必须以正斜杠“/”开头
假设您要匹配格式为 /user/12891
的 URL,该 URL 以 user
开头,并以数字结尾。
此 URL 的格式为:"/user/:userId{int}"
,其中 user
之后的数字表示为 :userId{int}
。
您要提取的每个参数都遵循 :paramName{baseType}
或 :paramName
形式。
paramName
与路由中的变量名称相同baseType
指示 paramName
的类型因此,没有显式基本类型的 :userId
将映射到路由中的 let userId: String
,而 :userId{int}
将映射到路由中的 let userId: Int
。
URL 定义必须以正斜杠“/”开头
基本类型“string”映射到 String
基本类型“int”映射到 Int
假设您要匹配格式为 /user?userId=12891
的 URL,该 URL 以 user
路径开头,并在参数中具有数字 userId
值。
此 URL 的格式为:"/user/?:userId{int}"
,其中 ?
之后的所有内容都是查询的一部分。
可选项
现在假设 userId
值是可选的,因此 /user&userId=12891
和 /user
将映射到同一路由。
此 URL 的格式为:"/user/?:userId{int?}"
字面量
现在假设我们要匹配格式为 /user&userId=12891&sourceType=email
的 URL,其中查询中存在('sourceType', 'email')对。
此 URL 的格式为:"/user/?:userId{int}&:sourceType{'email'}"
URL 格式必须用 & 分隔连续的查询参数
URL 定义 "/user/:userId{int}?:nickname{string}"
包含内置基本类型:"int"
,"string"
和 "bool"
。每个基本类型都映射到 Swift 类型
"int" -> Int
"string" -> String
"bool" -> Bool
任何基本类型都可以用方括号括起来,以表示逗号分隔的基本值列表。示例:
"[int]" -> [Int]
。
如果您想使用不是内置的基本类型怎么办?这是一个例子
(1) 定义一个名为“uint”的基本类型,该类型将值转换为 UInt
private struct CorridorTypeUInt: CorridorTypeProtocol {
let name = "uint"
func convertToType(from value: String) -> Any? {
return UInt(value)
}
}
基本类型名称必须是字母,并且不能与任何现有的内置基本类型名称相同
(2) 使用自定义基本类型实例化 CorridorTypes
let baseTypes = CorridorTypes(customTypes: [CorridorTypeUInt()])
(3) 使用 baseTypes 实例化路由器
let router = CorridorRouter(corridorTypes: baseTypes)
现在,让我们看看如何使用它
struct ViewUserProfile: CorridorRoute {
let userId: UInt
let nickname: String?
}
router.register("/user/:userId{uint}?:nickname{string}", { try corridorResponse($0, ViewUserProfile.self) })
(4) 使用 URL www.example.com/8971/?nickname=Bob
调用 attemptMatch(_:URL)
let result = router.attemptMatch(url)
let route = routeResponse.route as! ViewUserProfile
print(route.userId)
8971
瞧!
如果诸如跟踪 ID 或用户 ID 之类的查询参数包含在您的许多 URL 中,该怎么办?
假设您的 URL 包含一个查询参数 sourceUserId
,用于跟踪单击 URL 的用户。
您可以将 let sourceUserId: String?
包含在所有路由中,但是这样做既繁琐又容易出错。 CorridorGlobalParams 来救援!
(1) 创建一个符合 CorridorGlobalParams
的结构体
struct GlobalParams: CorridorGlobalParams {
let sourceUserId: String?
}
(2) 创建一个映射
let mapping = GlobalQueryOptionalParamsMapping(params: ["sourceUserId"],
decoder: { try corridorGlobalParamsResponse($0, GlobalParams.self) })
(3) 使用映射实例化路由器
let router = CorridorRouter(globalQueryOptionalParamsMapping: mapping)
现在,让我们看看如何使用它
(4) 使用 URL www.example.com/news_feed/?sourceUserId=abc123
调用 attemptMatch(_:URL)
let result = router.attemptMatch(url)
let globalParams = routeResponse.globalParams as! GlobalParams
print(globalParams.sourceUserId)
abc123
瞧!
Corridor 支持多种在项目中安装库的方法。
要使用 CocoaPods 将 Corridor 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
pod 'Corridor','~> 1.0.3'
也请在您的 Podfile
中单独添加一行
use_frameworks!
要使用 Carthage 将 Corridor 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "Nextdoor/corridor" ~> 1.0.3'
运行 carthage
来构建框架,并将构建的 Corridor.framework
拖到您的 Xcode 项目中。
要使用 Swift Package Manager 将 Corridor 集成到您的 Xcode 项目中,请将其添加到您的 Package.swift
的 dependencies
值中
dependencies: [
.package(url: "https://github.com/Nextdoor/corridor.git", from: "1.0.3")
]
Corridor 在 Apache 2.0 许可下发布。查看 LICENSE 了解详细信息。