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
```
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.
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.
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.
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.
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.
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`.
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.
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.
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.
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.