I'm writing some Swift code to parse filenames of video files and extract the show, season and episode. These are returned as key/value parts in a dictionary. As part of my unit tests, I found something odd. First the code (comments and whitespace removed):
public static func parse(_ filename: String, defaults: [String: String] = [String: String]()) -> [String: String] {
let url = URL(fileURLWithPath: filename)
let file = url.deletingPathExtension().lastPathComponent
if file.count == 0 {
return ret
}
if file.count > 0 {
ret["file"] = file
}
let ext = url.pathExtension
if ext.count > 0 {
ret["extension"] = ext
}
let path = url.deletingLastPathComponent().path
if path.count > 0 {
ret["path"] = path
}
I called this in my test case thus...
ParseVideoFilename.parse("non-empty-filename.m4v")
And this is what resulted:
["ext": "m4v", "file": "non-empty-filename", "path": "/private/tmp"]
I am a bit surprised about that path. I did not pass that in, so I assume URL is doing something here. I don't expand the path nor resolve it. Is this expected behavior, and if so, why?