Bug in appendingPathExtension

Has anyone else seen in Ventura B3 where you have a URL path as a source and you want to use the filename for that, aka lastPathComponent in a destination URL, so you have something like:

let destinationURL = destinationDIrectory.appendingPathExtension(sourceURL.lastPathComponent)

and lastPathComponent is a file name

but what you get for destinationURL is: destinationDirectory.lastPathComponent instead of destinationDirectory/lastPathComponent

or is it just me?

Replies

Bug filed: FB10616393

I’m confused by this:

let destinationURL = destinationDIrectory.appendingPathExtension(sourceURL.lastPathComponent)

A path extension is the text after the dot. For example, in this:

let sourceURL = URL(fileURLWithPath: "/Users/quinn/test.txt")

the path extension is txt. Given that, adding the last path component as a path extension is a type error. Consider this:

let destinationDirectory = URL(fileURLWithPath: "/Users/quinn/Test")
let destinationURL = destinationDirectory.appendingPathExtension(sourceURL.lastPathComponent)
print(destinationURL)

On current systems (macOS 12.4) this prints:

file:///Users/quinn/Test.test.txt/

which is syntactically correct but semantically nonsense.

My best guess is that you intended to call appendingPathComponent(_:), which yields:

file:///Users/quinn/Test/test.txt

which makes a lot more sense.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

that indeed do the trick, thanks! Now to let the person who created the thing I was using as a tutorial that they need to fix their stuff. Le joy.