Swift 3 文件复制错误

标签 swift url nsfilemanager

我正在尝试将 .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/

相关文章:

php - Google 报告了一些实际上并不存在的 404 错误。这是否表明我的网站被黑了?

ios - 为什么我的应用程序包没有创建文件夹?

ios - NSArray writeFileToPath 失败

iphone - URLWithString 返回 nil

ios - swift:如何打开 Safari 应用程序(其中没有链接)

ios - 在 whereKey 中解析 iOS Swift 对象键

optimization - SEO - 如何避免搜索引擎爬虫不读取整个 URL

ios - 如何在swift编程中在UIimage中设置背景颜色

包含闭包的 Swift 数组

java - 通过 HTTP 客户端编写curl 等效 java 代码时遇到问题