gpt4 book ai didi

cocoa - 如果我尝试将项目替换为不同的卷,replaceItemAtURL 将不起作用

转载 作者:行者123 更新时间:2023-12-03 16:18:36 32 4
gpt4 key购买 nike

在我的 cocoa 应用程序中,我有一个将文件 move 到特定目的地的功能。假设用户有一个文件/Users/Josh/Desktop/my.zip。用户可以将此 my.zip 文件 move 到另一个目的地,my.zip 将在桌面中消失。

在我的代码中,我使用了replaceItemAtURL,因为目标文件夹还可能包含另一个my.zip:

bMoveSuccess = [[NSFileManager defaultManager] replaceItemAtURL:destinationURL withItemAtURL:sourceURL backupItemName:nil options:0 resultingItemURL:&destinationURL error:&error];

如果我调用它来在同一卷中 move 文件,它可以工作,但是如果我调用它并将目标设置为不同的卷(即 USB key ),那么我会收到错误消息:

Error Domain=NSCocoaErrorDomain Code=512 "The file “my.zip” couldn’t be saved."
UserInfo=0x101b51840 {NSFileOriginalItemLocationKey=file://localhost/Volumes/USB/my.zip,
NSURL=file://localhost/Volumes/USB/my.zip,
NSFileNewItemLocationKey=file://localhost/Users/Josh/Desktop/my.zip,
NSUnderlyingError=0x101b2e3d0 "The operation couldn’t be completed. Cross-device link"}

最佳答案

这行不通。这些方法依赖于较新的 NSURL 书签功能,该功能可以跟踪文件的位置,即使文件在在同一卷中打开时发生 move 。它不适用于跨体积 move 。您必须改变您的方法(添加一些额外的代码来检查此错误并专门处理该情况)。

关于cocoa - 如果我尝试将项目替换为不同的卷,replaceItemAtURL 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366440/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com