You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ventura系统下copyItemAtPath行为是否变更?返回‘文件不存在’错误

问题描述

我的应用启动时会在Application Support文件夹创建工作文件,随后将其保存到用户偏好设置指定路径的文件夹中。保存代码如下:

- (void) saveFile: (NSString *) path {
    
    NSFileManager   *dm = [NSFileManager defaultManager];
    NSError         *error;
    
    if ([dm copyItemAtPath:filePath toPath:path error:&error]) {
        [dm removeItemAtPath:filePath error:&error];
    }
}

升级至MacOS 13.0 Ventura系统后,调用copyItemAtPath失败,报错信息如下:

Error Domain=NSCocoaErrorDomain Code=4 "The file “Working 20221113075654.rtf” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/jpurlia/Library/Application Support/My App/Working 20221113075654.rtf, NSUserStringVariant=(
    Copy
), NSDestinationFilePath=~/Documents/My App/Saved File - 20221113075729.rtf, NSFilePath=/Users/jpurlia/Library/Application Support/APBA Baseball/Working 20221113075654.rtf, NSUnderlyingError=0x6000026ff180 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

确认工作文件确实存在于Application Support文件夹的应用目录中,但代码在Ventura系统下失效,请问可能是什么原因?

可能的原因分析
  • 路径指向不一致:从报错信息能看到,NSSourceFilePathErrorKey对应的路径是/Users/jpurlia/Library/Application Support/My App/Working 20221113075654.rtf,但NSFilePath显示的路径却是/Users/jpurlia/Library/Application Support/APBA Baseball/Working 20221113075654.rtf,两个路径的应用目录名称不匹配,说明代码中的filePath变量可能指向了错误的目录,导致找不到目标文件。
  • 系统权限管控升级:MacOS 13 Ventura对App Sandbox和文件隐私权限的管控更严格。如果应用开启了App Sandbox,需要确保Info.plist中配置了访问Application Support和Documents文件夹的正确权限;另外,系统升级后可能需要重新获取用户对这些目录的访问授权。
  • 路径未完成解析:目标路径中的~/Documents/My App/...包含波浪号~,NSFileManager的copyItemAtPath:方法无法直接识别该符号,需要先通过stringByExpandingTildeInPath方法将路径展开为绝对路径后再传入。
  • 文件写入未同步:Ventura系统的文件系统操作可能存在延迟,应用刚创建完工作文件,磁盘写入还未完成就调用复制方法,此时系统会判定文件不存在。可以在复制前增加文件存在性检查,或者等待写入完成后再执行复制操作。
  • 目录获取方式错误:硬编码Application Support目录路径的代码,在Ventura系统中可能因为目录结构的细微变更失效。正确的做法是使用NSSearchPathForDirectoriesInDomains方法动态获取该目录路径,而非硬编码。

内容的提问来源于stack exchange,提问作者johnpurlia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 10:15:15