One Logo Pool Ball
Foundation · IOS API REFERENCE

URL.ParseStrategy

struct · iOS 16.0+ · SDK 27.1

struct ParseStrategy

A parse strategy for creating URLs from formatted strings. Create an explicit ``URL/ParseStrategy`` to parse multiple strings according to the same parse strategy. The following example creates a customized strategy, then applies it to multiple URL candidate strings. ```swift let strategy = URL.ParseStrategy( scheme: .defaultValue("https"), user: .optional, password: .optional, host: .required, port: .optional, path: .required, query: .required, fragment: .optional) let urlStrings = [ "example.com?key1=value1", // no scheme or path "https://example.com?key2=value2", // no path "https://example.com", // no query "https://example.com/path?key4=value4", // complete "//example.com/path?key5=value5" // complete except for default-able scheme ] let urls = urlStrings.map { try? strategy.parse($0) } // [nil, nil, nil, Optional(https://example.com/path?key4=value4), Optional(https://example.com/path?key5=value5)] ``` You don't need to instantiate a parse strategy instance to parse a single string. Instead, use the URL initializer ``URL/init(_:strategy:)``, passing in a string to parse and a customized strategy, typically created with one of the static accessors. The following example parses a URL string, with a custom strategy that provides a default value for the port component if the source string doesn't specify one. ```swift let urlString = "https://internal.example.com/path/to/endpoint?key=value" let url = try? URL(urlString, strategy: .url .port(.defaultValue(8080))) // https://internal.example.com:8080/path/to/endpoint?key=value ```

Members (11)

method · iOS 16.0+

host

TypeScript
host(strategy?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
func host(_ strategy: URL.ParseStrategy.ComponentParseStrategy<String> = .required) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's host component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the host component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

method · iOS 16.0+

path

TypeScript
path(strategy?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
func path(_ strategy: URL.ParseStrategy.ComponentParseStrategy<String> = .optional) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's path component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the path component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

method · iOS 16.0+

port

TypeScript
port(strategy?: URL_ParseStrategy_ComponentParseStrategy<number>): URL_ParseStrategy
Swift declaration
func port(_ strategy: URL.ParseStrategy.ComponentParseStrategy<Int> = .optional) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's port component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the port component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

method · iOS 16.0+

user

TypeScript
user(strategy?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
func user(_ strategy: URL.ParseStrategy.ComponentParseStrategy<String> = .optional) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's user component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the user component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

method · iOS 16.0+

parse

TypeScript
parse(value: string): string
Swift declaration
func parse(_ value: String) throws -> URL

Parses a URL string in accordance with this strategy and returns the parsed value. Use this method to repeatedly parse URL strings with the same ``URL/ParseStrategy``. To parse a single URL string, use the URL initializer ``URL/init(_:strategy:)``. This method throws an error if the parse strategy can't parse the provided string. - Parameter value: The string to parse. - Returns: The parsed URL value.

method · iOS 16.0+

query

TypeScript
query(strategy?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
func query(_ strategy: URL.ParseStrategy.ComponentParseStrategy<String> = .optional) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's query component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the query component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

init · iOS 16.0+

init

TypeScript
static init(scheme?: URL_ParseStrategy_ComponentParseStrategy<string>, user?: URL_ParseStrategy_ComponentParseStrategy<string>, password?: URL_ParseStrategy_ComponentParseStrategy<string>, host?: URL_ParseStrategy_ComponentParseStrategy<string>, port?: URL_ParseStrategy_ComponentParseStrategy<number>, path?: URL_ParseStrategy_ComponentParseStrategy<string>, query?: URL_ParseStrategy_ComponentParseStrategy<string>, fragment?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
init(scheme: URL.ParseStrategy.ComponentParseStrategy<String> = .required, user: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, password: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, host: URL.ParseStrategy.ComponentParseStrategy<String> = .required, port: URL.ParseStrategy.ComponentParseStrategy<Int> = .optional, path: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, query: URL.ParseStrategy.ComponentParseStrategy<String> = .optional, fragment: URL.ParseStrategy.ComponentParseStrategy<String> = .optional)

Creates a new `ParseStrategy` with the given configurations. - Parameters: - scheme: The strategy to use for parsing the `scheme`. - user: The strategy to use for parsing the `user`. - password: The strategy to use for parsing the `password`. - host: The strategy to use for parsing the `host`. - port: The strategy to use for parsing the `port`. - path: The strategy to use for parsing the `path`. - query: The strategy to use for parsing the `query`. - fragment: The strategy to use for parsing the `fragment`.

method · iOS 16.0+

scheme

TypeScript
scheme(strategy?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
func scheme(_ strategy: URL.ParseStrategy.ComponentParseStrategy<String> = .required) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's scheme component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the scheme component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

method · iOS 16.0+

fragment

TypeScript
fragment(strategy?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
func fragment(_ strategy: URL.ParseStrategy.ComponentParseStrategy<String> = .optional) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's fragment component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the fragment component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

method · iOS 16.0+

password

TypeScript
password(strategy?: URL_ParseStrategy_ComponentParseStrategy<string>): URL_ParseStrategy
Swift declaration
func password(_ strategy: URL.ParseStrategy.ComponentParseStrategy<String> = .optional) -> URL.ParseStrategy

Modifies a parse strategy to parse a URL's password component in accordance with the provided behavior. - Parameter strategy: A strategy for parsing the password component. - Returns: A modified ``URL/ParseStrategy`` that incorporates the specified behavior.

method · iOS 16.0+

consuming

TypeScript
consuming(input: string, index: SDKHandle, bounds: SDKHandle): ({ "upperBound": SDKHandle; "output": string }) | null
Swift declaration
func consuming(_ input: String, startingAt index: String.Index, in bounds: Range<String.Index>) throws -> (upperBound: String.Index, output: URL)?

Processes the input string within the specified bounds, beginning at the given index, and returns the end position of the match and the produced output. Don't call this method directly. Regular expression matching and capture calls it automatically when matching substrings. - Parameters: - input: An input string to match against. - index: The index within `input` at which to begin searching. - bounds: The bounds within `input` in which to search. - Returns: The upper bound where the match terminates and a matched instance, or `nil` if there isn't a match.

Skipped declarations

No declarations were skipped in this snapshot.

All Foundation types →