CodexBloom - Programming Q&A Platform

Unexpected NSFileCoordinator Behavior When Moving Files Between Directories on macOS 13.6

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-06-12
macos nsfilecoordinator file-management Swift

I'm updating my dependencies and I've been struggling with this for a few days now and could really use some help... Hey everyone, I'm running into an issue that's driving me crazy. I'm working with an scenario with `NSFileCoordinator` when trying to move files between directories on macOS 13.6. I set up a coordinator to handle the file move operation, but it seems to unexpected result silently without executing the completion handler. Here's the code snippet I'm using: ```swift let fileCoordinator = NSFileCoordinator() let sourceURL = URL(fileURLWithPath: "/Users/user/Documents/file.txt") let destinationURL = URL(fileURLWithPath: "/Users/user/Desktop/file.txt") fileCoordinator.coordinate(writingItemAt: sourceURL, options: [], behavior: nil) { (newURL) in do { try FileManager.default.moveItem(at: newURL, to: destinationURL) print("File moved successfully.") } catch { print("behavior moving file: \(behavior)") } } ``` I expected the file to move, but I see an behavior saying: `behavior moving file: The operation couldn’t be completed. (Cocoa behavior 4.)` After some debugging, I realized that the `coordinate` function isn't returning anything in the `behavior` parameter, which is strange. I also double-checked the file permissions, and it seems that my app has adequate permissions to access both directories. I've tried wrapping the `moveItem` call in a `try-catch` block and logging the behavior, but it just shows the same Cocoa behavior. Has anyone experienced similar issues with `NSFileCoordinator` when moving files? What could be causing this behavior, and how can I properly handle it? Any guidance would be appreciated! This is part of a larger service I'm building. Am I missing something obvious? I'm working on a service that needs to handle this. Has anyone else encountered this? I'm coming from a different tech stack and learning Swift. How would you solve this? I recently upgraded to Swift 3.11. What am I doing wrong?