URLPatterns 0.2.0

URLPatterns 0.2.0

测试测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2017 年 10 月
SwiftSwift 版本4.0
SPM支持 SPM

John Morgan维护。



🎯URLPatterns🎯



URLPatterns 是一个小的库,它允许更符合 Swift 进行 URL 路径元素的模式匹配。

URL 扩展了新方法 countedPathElements(),它将 URL 的路径元素数组转换为 Counted 枚举。每个 Counted 项都有一个不同数量的关联值,这使得对每个元素的匹配变得更加容易。

if case .n4("user", let userId, "profile", _) = url.countedPathElements() {
    // show profile for userId
}

Counted 使我们能够匹配带任何数量的元素的路径,并支持其关联值上的 表达式通配符值绑定 模式。它可以基于 BeginsEnds 进行匹配,这些匹配仅基于第一个/最后一个元素,甚至可以基于正则表达式匹配特定路径元素。以下是一个示例 DeepLink 枚举,它有一个可失败初始化器,该初始化器接受一个 URL

enum DeepLink {

    case home, history, settings, terms, news, contact
    case chat(room: String)
    case profile(userId: String)
}

extension DeepLink {

    init?(url: URL) {

        guard url.scheme == "myscheme" else { return nil }
        guard url.host == "myhost" else { return nil }

        switch url.countedPathComponents() {

        case .n0, .n1(""):                          self = .home
        case .n1("history"):                        self = .history
        case .n2(_, "settings"):                    self = .settings
        case .n2("chat", let room):                 self = .chat(room: room)
        case .n3("users", let userId, "profile"):   self = .profile(userId: userId)
        case .n1(Regex(contact.*))                  self = .contact
        case Begins("news", "latest"):              self = .news
        case Ends("terms"):                         self = .terms		
        default:                                    return nil
        }
    }
}

安装

URLPatterns 通过 CocoaPods 提供。要安装,
只需将以下行添加到您的 Podfile 中。

pod "URLPatterns"

许可证

URLPatterns 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。