Let's assume that I have three sets containing extensions:
let photos: Set = ["jpg", "png", "tiff"]
let videos: Set = ["mp4", "mov", "mkv"]
let audios: Set = ["mp3", "wav", "wma"]
and a simple enum as:
enum FileType {
case photo, video, audio, unknown
}
Now what I want to do is to implement a function that returns FileType
option based on what is the passed string to it and which set contains it:
func getType(of file: String) -> FileType {
if photos.contains(file) { return .photo }
if videos.contains(file) { return .video }
if audios.contains(file) { return .audio }
return .unknown
}
It should work as expected, but I wonder if there is an approach to transform the if
statement to one switch
case (even if it would change the logic a bit), especially when working with enums the switch
statement(s) is a better choice to avoid errors.
If it is unachievable by using switch
statement, I would also appreciate any elegant alternative(s).
FileExtension
type that has afileType
property with the corresponding file type, e.g gist.github.com/hamishknight/c211c63e321df33d18e9f90c72d4a20f – Eisteddfod