gpt4 book ai didi

Swift 3 文件复制错误

转载 作者:行者123 更新时间:2023-11-30 11:56:52 25 4
gpt4 key购买 nike

我正在尝试将 .plist 的副本复制到 Documents 文件夹中。我有这段代码,但我收到“文件复制错误”。

let srcPath = Bundle.main.path(forResource: "Gameboard", ofType: "plist")
//let path = Bundle.main.resourcePath!
let path = String(describing: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first)
print(srcPath)
print(path)
do {
//Copy the project plist file to the documents directory.
try FileManager.default.copyItem(atPath: srcPath!, toPath: path)
} catch {
print("File copy error!")
}

最佳答案

Documentation :

dstPath
The path at which to place the copy of srcPath. This path must include the name of the file or directory in its new location. This parameter must not be nil.

您需要将文件名“Gameboard.plist”附加到目标路径:

let srcUrl = Bundle.main.url(forResource: "Gameboard", ofType: "plist")
let documentsUrl = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
).first!
let destinationUrl = documentsUrl.appendingPathComponent(
"Gameboard.plist", // or srcUrl.lastPathComponent
isDirectory: false
)

do {
try FileManager.default.copyItem(at: srcUrl, to: destinationUrl)
} catch {
...
}

关于Swift 3 文件复制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47719821/

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