Contents

format(_:)

Formats a URL, using this style.

Declaration

func format(_ value: URL) -> String

Parameters

  • value:

    The URL to format.

Return Value

A string representation of value, formatted according to the style’s configuration.

Discussion

Use this method when you want to create a single style instance, and then use it to format multiple URL instances. The following example creates a custom format style and then uses it to format a variety of URLs in an array:

let style = URL.FormatStyle(
    scheme: .never,
    user: .never,
    password: .never,
    host: .omitSpecificSubdomains(["www", "mobile", "m."],
                                  includeMultiLevelSubdomains: true),
    port: .never,
    path: .always,
    query: .never,
    fragment: .never)
let urls = [
    URL(string: "https://www.example.com/path/one")!,
    URL(string: "https://beta.example.com/path/two")!,
    URL(string: "https://beta.staging.west.example.com/three")!,
    URL(string: "https://query.example.com/four?key4=value4")!
]
let formatted = urls.map { $0.formatted(style) } // ["example.com/path/one", "beta.example.com/path/two", "west.example.com/three", "query.example.com/four"]

To format a single floating-point value, use the URL instance method formatted(_:) method passing in an instance of URL.FormatStyle, or formatted() to use a default style.