Swiftpack.co -  ShezHsky/URLDecoder as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Decode URL types into native Swift structures
.package(url: "https://github.com/ShezHsky/URLDecoder.git", from: "0.0.2")


Deserialize URLs into instances of your types.


The URLDecoder package provides a Swift Decoder implementation capable of deserializing a well-formed URL into a native Swift type. This is a convenient alternative to hand-crafting the same decoding routine using an instance of URLComponents directly, such as during the handling a deep link within your app.

Consider the following GroceryProduct Swift structure, that conforms to the Decodable protocol:

struct GroceryProduct: Decodable {
     var identifier: Int
     var name: String
     var points: Int

With no further code changes, the following URLs that provide key-value semantics will be decoded into the structure:

https://grocery.shopping.co.uk/product/identifier/1/name/Durian/points/600 https://grocery.shopping.co.uk/product?identifier=1&name=Durian&points=600

Alternativley, there may be situations where your existing systems do not provide key-value semantics in its URLs. For example, the following URL:


Represents the same data as above, but has dropped the keys necessary to decode the structure. By customising the init(decoder:) function of the GroceryProduct to use an unkeyed container, the URL can continue to be decoded:

struct GroceryProduct: Codable {
    init(decoder: Decoder) {
        var container = try decoder.unkeyedContainer()
        let type = try container.decode(String.self)
        guard type == "product" else { throw CustomDecodingError() }
        identifier = try container.decode(Int.self)
        name = try container.decode(String.self)
        points = try container.decode(Int.self)

Note that this couples the decoding implementation to the order of values in the URL, so be mindful when dipping into unkeyed territory.

Further decoding examples are available within the Tests folder.


This package is a work in progress, with certain decoding pathways remaining unimplemented (as they are not exercised in apps using this package!). Feel free to submit PRs to fill them in if you find this package useful.


Stars: 0
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API