<!--
{
  "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/formatted(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation3URLV9formattedy12FormatOutputQzxAA0D5StyleRzAC0D5InputRtzlF"
  },
  "title" : "formatted(_:)"
}
-->

# formatted(_:)

Formats the URL, using the provided format style.

```
func formatted<F>(_ format: F) -> F.FormatOutput where F : FormatStyle, F.FormatInput == URL
```

## Parameters

`format`

The format style to apply when formatting the URL.

## Return Value

A formatted string representation of the URL.

## Discussion

Use this method when you want to format a single URL value with a specific format style, or call it repeatedly with different format styles. The following example uses the static accessor [`url`](/documentation/Foundation/FormatStyle/url) to get a default style, then modifies its behavior to include or omit different URL components when [`formatted(_:)`](/documentation/Foundation/URL/formatted(_:)) creates the string:

```swift
let url = URL(string:"https://www.example.com:8080/path/to/endpoint?key=value")!
let formatted = url.formatted(.url
    .scheme(.never)
    .host(.always)
    .port(.never)
    .path(.always)
    .query(.never)) // "www.example.com/path/to/endpoint"
```

---

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)