<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URL/ParseStrategy",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation3URLV13ParseStrategyV"
  },
  "title" : "URL.ParseStrategy"
}
-->

# URL.ParseStrategy

A parse strategy for creating URLs from formatted strings.

```
struct ParseStrategy
```

## Overview

Create an explicit [`URL.ParseStrategy`](/documentation/Foundation/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 [`init(_:strategy:)`](/documentation/Foundation/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
```

## Topics

### Creating a URL parse strategy

[`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<Int>, path: URL.ParseStrategy.ComponentParseStrategy<String>, query: URL.ParseStrategy.ComponentParseStrategy<String>, fragment: URL.ParseStrategy.ComponentParseStrategy<String>)`](/documentation/Foundation/URL/ParseStrategy/init(scheme:user:password:host:port:path:query:fragment:))

Creates a URL parse strategy with the specified component-parsing behaviors.

[`enum ComponentParseStrategy`](/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy)

The strategy used to parse one component of a URL.

### Customizing strategy behavior

[`func scheme(URL.ParseStrategy.ComponentParseStrategy<String>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/scheme(_:))

Modifies a parse strategy to parse a URL’s scheme component in accordance with the provided behavior.

[`func user(URL.ParseStrategy.ComponentParseStrategy<String>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/user(_:))

Modifies a parse strategy to parse a URL’s user component in accordance with the provided behavior.

[`func password(URL.ParseStrategy.ComponentParseStrategy<String>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/password(_:))

Modifies a parse strategy to parse a URL’s password component in accordance with the provided behavior.

[`func host(URL.ParseStrategy.ComponentParseStrategy<String>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/host(_:))

Modifies a parse strategy to parse a URL’s host component in accordance with the provided behavior.

[`func port(URL.ParseStrategy.ComponentParseStrategy<Int>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/port(_:))

Modifies a parse strategy to parse a URL’s port component in accordance with the provided behavior.

[`func path(URL.ParseStrategy.ComponentParseStrategy<String>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/path(_:))

Modifies a parse strategy to parse a URL’s path component in accordance with the provided behavior.

[`func query(URL.ParseStrategy.ComponentParseStrategy<String>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/query(_:))

Modifies a parse strategy to parse a URL’s query component in accordance with the provided behavior.

[`func fragment(URL.ParseStrategy.ComponentParseStrategy<String>) -> URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy/fragment(_:))

Modifies a parse strategy to parse a URL’s fragment component in accordance with the provided behavior.

[`enum ComponentParseStrategy`](/documentation/Foundation/URL/ParseStrategy/ComponentParseStrategy)

The strategy used to parse one component of a URL.

### Parsing strings

[`func parse(String) throws -> URL`](/documentation/Foundation/URL/ParseStrategy/parse(_:))

Parses a URL string in accordance with this strategy and returns the parsed value.

### Locating URLs with regular expressions

[`func consuming(String, startingAt: String.Index, in: Range<String.Index>) throws -> (upperBound: String.Index, output: URL)?`](/documentation/Foundation/URL/ParseStrategy/consuming(_:startingAt:in:))

Process the input string within the specified bounds, beginning at the given index, and return the end position (upper bound) of the match and the produced output.

### Applying date-parsing strategies

### Supporting Types

[`typealias RegexOutput`](/documentation/Foundation/URL/ParseStrategy/RegexOutput)

The type returned when capturing matching substrings with this strategy.

### Default Implementations

[CustomConsumingRegexComponent Implementations](/documentation/Foundation/URL/ParseStrategy/CustomConsumingRegexComponent-Implementations)

[ParseStrategy Implementations](/documentation/Foundation/URL/ParseStrategy/ParseStrategy-Implementations)

[RegexComponent Implementations](/documentation/Foundation/URL/ParseStrategy/RegexComponent-Implementations)

## Relationships

### Conforms To

[`RegexComponent`](/documentation/Swift/RegexComponent)

[`Copyable`](/documentation/Swift/Copyable)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomConsumingRegexComponent`](/documentation/Swift/CustomConsumingRegexComponent)

[`Decodable`](/documentation/Swift/Decodable)

[`Escapable`](/documentation/Swift/Escapable)

[`Encodable`](/documentation/Swift/Encodable)

[`ParseStrategy`](/documentation/Foundation/ParseStrategy)

[`Equatable`](/documentation/Swift/Equatable)

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)