gpt4 book ai didi

ios - 尝试使用 FileManager 移动项目时出错

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:09 24 4
gpt4 key购买 nike

我有一些项目保存在文档目录中。我目前需要将它们以编程方式移动到另一个目录。我成功创建了新目录,但在使用 FileManager.default.moveItem 时它似乎并没有查看它。

用于创建目录的代码。

let path = getDocumentsDirectory().appendingPathComponent("Media").path

do{
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)

print("\nDIRECTORY 'Media' CREATED")
}catch {
print("\nDIRECTORY 'Media' WAS NOT ABLE TO BE CREATED")
print("ERROR - \(error)")
}

我使用此代码检查我的 URL。

let mediaURL = getDocumentsDirectory().appendingPathComponent("Media")
print("\nMEDIA URL: \(mediaURL)")

输出这个。

MEDIA URL:file:///Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/Media/

这是我用来移动项目的代码。

do{
try FileManager.default.moveItem(at: url, to: mediaURL)

print("\nFILE MOVED TO NEW MEDIA PATH SUCCESSFULLY")
}catch {
print("\nCOULDN'T MOVE FILE TO NEW MEDIA PATH")
print("ERROR - \(error)")
}

这是我在尝试移动的每个项目时收到的错误。

无法将文件移动到新媒体路径错误 - 错误域 = NSCocoaErrorDomain 代码 = 516 “无法将“image_5”移动到“文档”,因为已存在同名项目。” UserInfo = {NSSourceFilePathErrorKey =/Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/image NSUserStringVariant=( 移动), NSDestinationFilePath=/Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/Media, NSFilePath =/Users/joseph/Library/Developer/CoreSimulator/Devices/552B72BF-8929-40EE-A75B-4574D3D2918A/data/Containers/Data/Application/E168B872-6A1D-4D78-B460-D45194088E5B/Documents/image_5, NSUnderlying0ff0=0x6错误域=NSPOSIXErrorDomain 代码=17“文件存在”}}

媒体目录应该是空的,因为它是新创建的,所以我收到的错误有点令人困惑。它还说它正在尝试将其移至“文档”,但它应该移至“媒体”。导致此问题的原因是什么,我该如何解决?我只是想将保存在“文档”中的项目移动到新目录“媒体”。

最佳答案

您必须将文件名附加到目标 URL。根据文档:

The new location for the item in srcURL. The URL in this parameter must not be a file reference URL and must include the name of the file or directory in its new location. This parameter must not be nil.

关于ios - 尝试使用 FileManager 移动项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50012688/

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