MediaType

public struct MediaType : CustomStringConvertible

The media type (formerly known as MIME type) is a standardized way to indicate the nature and format of a document. This struct consists of a catagorical topLevelType and specific subtype seperated by a / (e.g. text/plain). In HTTP, The media type is sent as the first section of the Content-Type header and is case insensitive.

Usage Example:

let mediaType = MediaType(type: .application, subtype: "json")
print(mediaType.description)
// Prints ("application/json")
  • The topLevelType represents the category of the MediaType (e.g. audio).

    Declaration

    Swift

    public let topLevelType: TopLevelType
  • The subtype is the specific MediaType (e.g. html).

    Declaration

    Swift

    public let subType: String
  • Initialize a MediaType instance with a TopLevelType type and String subtype. If no subtype is provided it will default to * representing all subtypes.

    Usage Example:

    let mediaType = MediaType(type: .application, subtype: "json")
    

    Declaration

    Swift

    public init(type: TopLevelType, subType: String = "*")
  • Initialize a MediaType instance from a String. If no subtype is provided it will default to * representing all subtypes. If the string preceding the first / is not a valid TopLevelType the initializer will return nil.

    Usage Example:

    let mediaType = MediaType("application/json")
    

    Declaration

    Swift

    public init?(_ mimeType: String)
  • Helper constructor for the application/json media type

    Usage Example:

    let mediaType = MediaType.json
    print(mediaType.description)
    // Prints ("application/json")
    

    Declaration

    Swift

    public static let json: MediaType
  • Helper constructor for the application/x-www-form-urlencoded media type

    Usage Example:

    let mediaType = MediaType.urlEncoded
    print(mediaType.description)
    // Prints ("application/x-www-form-urlencoded")
    

    Declaration

    Swift

    public static let urlEncoded: MediaType
  • Returns the media type, as a String structured: topLevelType/subtype. Required for CustomStringConvertible conformance.

    Usage Example:

    print(mediaType.description)
    // Prints ("application/json")
    

    Declaration

    Swift

    public let description: String
  • Compares two MediaTypes returning true if they are equal. Required for Equatable conformance.