docs/Classes/ContentType.html
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.
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")
`
sharedInstance
`
Shared singleton instance.
Swift
public static let sharedInstance: ContentType
`
getContentType(forExtension:)
`
Get the content type for the given file extension.
let contentType = ContentType.sharedInstance
let result = contentType.getContentType(forExtension: "js")
print(String(describing: result))
//Prints Optional("application/javascript")
Swift
public func getContentType(forExtension ext: String) -> String?
| forExtension |
The file extension.
|
An Optional String for the content type.
`
getContentType(forFileName:)
`
Get the content type for the given file based on its extension.
let contentType = ContentType.sharedInstance
let result = contentType.getContentType(forFileName: "test.html")
print(String(describing: result))
//Prints Optional("text/html")
Swift
public func getContentType(forFileName fileName: String) -> String?
| forFileName |
The file name.
|
An Optional String for the content type.
`
isContentType(_:ofType:)
`
Check if the message content type matches the type descriptor.
let contentType = ContentType.sharedInstance
var result = contentType.isContentType("application/json", ofType: "json")
print(String(describing: result))
//Prints true
Swift
public func isContentType(_ messageContentType: String, ofType typeDescriptor: String) -> Bool
| messageContentType |
The content type.
|
| ofType |
The description of the type.
|
True if the types matched.