ContentType

public class ContentType

The ContentType class provides functions to determine the MIME content type for a given file extension. The user can pass in a complete file name e.g. foo.png or just the file extension e.g. png, or they can pass in both a MIME content type and a file extension and query whether they match.

Usage Example:

In this example, a ContentType instance is initialised called contentType. This instance is then used to obtain the MIME content type of the file foo.png, which is identified as image/png.

let contentType = ContentType.sharedInstance
let result = contentType.getContentType(forFileName: "foo.png")
print(String(describing: result))
// Prints Optional("image/png")
  • Shared singleton instance.

    Declaration

    Swift

    public static let sharedInstance = ContentType()
  • Get the content type for the given file extension.

    Usage Example:

    let contentType = ContentType.sharedInstance
    let result = contentType.getContentType(forExtension: "js")
    print(String(describing: result))
    //Prints Optional("application/javascript")
    

    Declaration

    Swift

    public func getContentType(forExtension ext: String) -> String?

    Parameters

    forExtension

    The file extension.

    Return Value

    An Optional String for the content type.

  • Get the content type for the given file based on its extension.

    Usage Example:

    let contentType = ContentType.sharedInstance
    let result = contentType.getContentType(forFileName: "test.html")
    print(String(describing: result))
    //Prints Optional("text/html")
    

    Declaration

    Swift

    public func getContentType(forFileName fileName: String) -> String?

    Parameters

    forFileName

    The file name.

    Return Value

    An Optional String for the content type.

  • Check if the message content type matches the type descriptor.

    Usage Example:

    let contentType = ContentType.sharedInstance
    var result = contentType.isContentType("application/json", ofType: "json")
    print(String(describing: result))
    //Prints true
    

    Declaration

    Swift

    public func isContentType(_ messageContentType: String, ofType typeDescriptor: String) -> Bool

    Parameters

    messageContentType

    The content type.

    ofType

    The description of the type.

    Return Value

    True if the types matched.