MediaType

public enum MediaType
extension MediaType: CustomStringConvertible
extension MediaType: RawRepresentable
extension MediaType: ExpressibleByStringLiteral
extension MediaType: Hashable

A type-safe representation of Media Types (or formerly known as MIME types).

You can create a media type in a type-safe manner using one of the possible cases. You can also create media type instances simply using string literals.

let mediaType: MediaType = "application/json" // is equivalent to
MediaType.application(.json())

Media type suffixes and parameters are supported both via string literals and MediaType cases.

let mediaType: MediaType = "application/atom; charset=utf-8" // is equivalent to
MediaType.application(.atom(nil, ["charset": "utf-8"]))

let mediaType: MediaType = "application/atom+xml" // is equivalent to
MediaType.application(.atom(.xml))

let mediaType: MediaType = "application/atom+xml; charset=utf-8" // is equivalent to
MediaType.application(.atom(.xml, ["charset": "utf-8"]))

You can create media type trees using either the string literal syntax, or using the other case of a particular media type.

"application/vnd.efi.img" // is equivalent to
MediaType.application(.other("vnd.efi.img"))
  • Represents the application media type.

    Represents binary data. Common examples: application/json, application/octet-stream.

    For the whole family of application media types consult the official IANA documentation.

    Declaration

    Swift

    case application(Application)
  • Represents the audio media type.

    Represents audible data. Common examples: audio/ac3, audio/mpeg.

    For the whole family of audio media types consult the official IANA documentation.

    Declaration

    Swift

    case audio(Audio)
  • Represents the font media type.

    Represents font or typeface data. Common examples: font/woff, font/ttf.

    For the whole family of font media types consult the official IANA documentation.

    Declaration

    Swift

    case font(Font)
  • Represents the image media type.

    Represents image or graphical data. This includes bitmap and vector images, along with animated image formats. Common examples: image/jpeg, image/apng.

    For the whole family of image media types consult the official IANA documentation.

    Declaration

    Swift

    case image(Image)
  • Represents the message media type.

    Represents embedded message data. Common examples: message/rfc882, message/http.

    For the whole family of message media types consult the official IANA documentation.

    Declaration

    Swift

    case message(Message)
  • Represents the model media type.

    Represents 3D modelling data. Common examples: model/step, model/3mf.

    For the whole family of model media types consult the official IANA documentation.

    Declaration

    Swift

    case model(Model)
  • Represents the multipart media type.

    Represents data formed from multiple components, which may have their individual media types. Common examples: multipart/form-data, multipart/encrypted.

    For the whole family of multipart media types consult the official IANA documentation.

    Declaration

    Swift

    case multipart(Multipart)
  • Represents the text media type.

    Represents textual only data. Common examples: text/css, text/html.

    For the whole family of text media types consult the official IANA documentation.

    Declaration

    Swift

    case text(Text)
  • Represents the video media type.

    Represents video data. Common examples: video/mp4, video/H264.

    For the whole family of video media types consult the official IANA documentation.

    Declaration

    Swift

    case video(Video)
  • Represents a custom media type that is currently not officially defined.

    Represents a custom media type with the given type and subtype. Optionally, you can specify a Suffix and Parameters.

    Declaration

    Swift

    case other(type: CustomStringConvertible, subtype: CustomStringConvertible, _: Suffix? = nil, _: Parameters? = nil)
  • Represents a wildcard media type.

    A wildcard media type has a type of *. A few examples:

    MediaType.anything(.anything()) // Creates: */*
    MediaType.anything(.other("dialog")) // Creates: */dialog
    MediaType.anything(.other("response", .xml)) // Creates: */response+xml
    

    Declaration

    Swift

    case anything(Anything)
  • The textual representation of the media type.

    The string form of a media type follows the pattern: type/subtype[+suffix][;parameters]. A few examples:

    MediaType.text(.css()).description // Outputs: text/css
    MediaType.audio(.ac3(nil, ["rate": 32000])).description // Outputs: audio/ac3;rate=32000
    MediaType.application(.atom(.xml, ["charset": "utf-8"])).description // Outputs: application/atom+xml;charset=utf-8
    

    Declaration

    Swift

    public var description: String { get }
  • Creates a media type from its raw string value.

    Declaration

    Swift

    public init(rawValue: String)

    Parameters

    rawValue

    The raw string value.

  • The raw string value of the media type.

    Declaration

    Swift

    public var rawValue: String { get }
  • Creates a media type from a string literal.

    Do not call this initializer directly. This rather allows you to use a string literal where you have to provide a MediaType node.

    Declaration

    Swift

    public init(stringLiteral value: String)
  • Returns a Boolean indicating whether two media types are the same.

    Two media types represent the same thing, and thus considered to be equal, if they have the same type, subtype, Suffix and Parameters.

    Declaration

    Swift

    public static func == (lhs: `Self`, rhs: `Self`) -> Bool
  • Declaration

    Swift

    public func hash(into hasher: inout Hasher)