Added some examples to treat MediaType as Hashable.

This commit is contained in:
Tamás Kálcza 2022-03-21 15:14:20 +01:00
parent 6507a4b47a
commit 75037aa479
1 changed files with 21 additions and 0 deletions

View File

@ -74,3 +74,24 @@ request.setValue("Content-Type", forHTTPHeaderField: "\(contentType)")
let (_, response) = try await URLSession.shared.data(for: request)
```
### Media Types are [`Hashable`](https://developer.apple.com/documentation/swift/hashable)
This means you can use ``MediaType``s in sets or dictionaries. For example, you can define the type of images your
application supports like so:
```swift
let supportedImages: Set<MediaType> = [.image(.png()), .image(.gif()), .image(.jpeg())]
```
### Comparing Media Types
You can also compare media type for equality using the ``MediaType/==(lhs:rhs:)`` operator.
```swift
func canHandle(response: URLResponse) -> Bool {
guard let mimeType = response.mimeType else { return false }
let mediaType = MediaType(rawValue: mimeType)
return mediaType == .application(.json())
}
```