It would help if you were more specific about the code you wrote, the directory you chose the file from, and the error you got. Failing that, I wrote a small test project to exercise this case. The code is pasted in at the end.
When I ran this on 10.15 and choose a file from the desktop, it’s able to successfully copy it. I ran this outside of Xcode, just to make sure the debugger wasn’t affecting things. In Console I saw two log entries:
default
02:15:27.629730-0700
Test125301
QQQ will copy /Users/quinn/Desktop/tmp.txt to /var/folders/ks/l3tlbq5x5gj6ycrwg6sv81jc0000gp/T/Test125301-300323C6-2DEE-4D19-8662-31FA68AD0343/tmp.txt
default
02:15:27.729106-0700
Test125301
QQQ did copy /Users/quinn/Desktop/tmp.txt to /var/folders/ks/l3tlbq5x5gj6ycrwg6sv81jc0000gp/T/Test125301-300323C6-2DEE-4D19-8662-31FA68AD0343/tmp.txt
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
@IBAction func testAction(_ sender: Any) {
let panel = NSOpenPanel()
panel.beginSheetModal(for: self.window) { response in
if response == .OK {
let sourceURL = panel.url!
let fm = FileManager.default
let tmpDir = fm.temporaryDirectory
let targetDir = tmpDir.appendingPathComponent("Test125301-\(UUID().uuidString)")
let targetURL = targetDir.appendingPathComponent(sourceURL.lastPathComponent)
do {
NSLog("QQQ will copy %@ to %@", sourceURL.path, targetURL.path)
try fm.createDirectory(at: targetDir, withIntermediateDirectories: false, attributes: nil)
try fm.copyItem(at: sourceURL, to: targetURL)
NSLog("QQQ did copy %@ to %@", sourceURL.path, targetURL.path)
} catch {
NSLog("QQQ something failed, error: %@", "\(error)")
}
}
}
}