MediaType
public struct MediaType: CustomStringConvertible, Equatable, Hashable
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")
-
An enum of all recognized top-level media types (categories). See: https://www.iana.org/assignments/media-types/media-types.xhtml
See moreDeclaration
Swift
public enum TopLevelType: String
-
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 aTopLevelType
type andString
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 aString
. If no subtype is provided it will default to*
representing all subtypes. If the string preceding the first/
is not a validTopLevelType
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 typeUsage Example:
let mediaType = MediaType.json print(mediaType.description) // Prints ("application/json")
Declaration
Swift
public static let json = MediaType(type: .application, subType: "json")
-
Helper constructor for the
application/x-www-form-urlencoded
media typeUsage Example:
let mediaType = MediaType.urlEncoded print(mediaType.description) // Prints ("application/x-www-form-urlencoded")
Declaration
Swift
public static let urlEncoded = MediaType(type: .application, subType: "x-www-form-urlencoded")
-
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.
Declaration
Swift
public static func == (lhs: MediaType, rhs: MediaType) -> Bool
-
The hashValue for the MediaTypes. Required for Hashable conformance.
Declaration
Swift
public let hashValue: Int