Skip to content

Instantly share code, notes, and snippets.

@perpeer
Last active March 7, 2020 10:19
Show Gist options
  • Select an option

  • Save perpeer/1ea65983f27b10a165837bee4b69a251 to your computer and use it in GitHub Desktop.

Select an option

Save perpeer/1ea65983f27b10a165837bee4b69a251 to your computer and use it in GitHub Desktop.
EnumAssociatedValues
import UIKit
typealias MimeTypeIdentifier = String
enum DefaultMimeType: MimeTypeIdentifier {
case pdf = "application/pdf"
case jpeg = "application/jpeg"
}
enum MimeType {
case Default(DefaultMimeType)
case Custom(MimeTypeIdentifier)
var getValue: String {
switch self {
case .Custom(let value):
return value
case .Default(let value):
return value.rawValue
}
}
}
let mimeTypePdf = MimeType.Default(.pdf)
let mimeTypeJpeg = MimeType.Default(.jpeg)
let mimeTypeCustom = MimeType.Custom("application/mp4")
print("MineTypePdf: \(mimeTypePdf.getValue)")
print("MineTypeJpeg: \(mimeTypeJpeg.getValue)")
print("MineTypeCustom: \(mimeTypeCustom.getValue)")
/*
**Print**
MineTypePdf: application/pdf
MineTypeJpeg: application/jpeg
MineTypeCustom: application/mp4
**Resource**
MIME types (IANA media types): [https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types](url)
List at IANA: [https://www.iana.org/assignments/media-types/media-types.xhtml#application](url)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment