URL.path returns forward slashes instead of backslashes on Windows
print(URL(fileURLWithPath: #"C:\Users\alex"#).path)
print(URL(fileURLWithPath: #"C:/Users/alex"#).path)
Both print statements above print C:/Users/alex. I think they should print C:\Users\alex.
Maybe worth noting: Using withUnsafeFileSystemRepresentation returns backslashes. Should that be exposed through URL.path?
URL(fileURLWithPath: #"C:/Users/alex"#).withUnsafeFileSystemRepresentation {
print(String(cString: $0!))
}
// prints C:\Users\alex
Based on my previous experience in this area, it's expected behavior.
If you want the file path associated with a file URL, you must use withUnsafeFileSystemRepresentation. path quite specifically refers to the path component of the URL per RFC8089, which is conceptually not the same thing.
In fact, I'd have expected your example to print /C:/Users/alex (with an extra leading slash), as that's that the RFC specifies. Not sure if the Foundation team changed that to remove the leading slash VERY recently... (EDIT: Yeah: #964)