无法将文件从捆绑包复制到iOS中的文档目录 [英] Can`t copy file from bundle to documents directory in iOS
问题描述
我正在尝试使用以下代码将文件从文件包复制到iOS中的documents目录中。
I am trying to copy a file from my bundle to the documents directory in iOS with the following code.
let bundlePath = NSBundle.mainBundle().pathForResource("information", ofType: ".png")
print(bundlePath, "\n") //prints the correct path
let destPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
let fileManager = NSFileManager.defaultManager()
let fullDestPath = NSURL(fileURLWithPath: destPath).URLByAppendingPathComponent("information.png")
let fullDestPathString = String(fullDestPath)
print(fileManager.fileExistsAtPath(bundlePath!)) // prints true
do{
try fileManager.copyItemAtPath(bundlePath!, toPath: fullDestPathString)
}catch{
print("\n")
print(error)
}
错误域= NSCocoaErrorDomain代码= 4文件 information.png不存在。 UserInfo = {NSSourceFilePathErrorKey = /用户/ macbookpro / Library / Developer / CoreSimulator / Devices / E58CA1C6-C6F1-4D72-9572-3925675E78A5 / data / Containers / Bundle / Application / EFA83E02-5F24-4BB3-B32A-7E755081A730 / AutoLayout tuts.app /information.png、NSUserStringVariant=(
复制
),NSDestinationFilePath = file:/// Users / macbookpro / Library / Developer / CoreSimulator / Devices / E58CA1C6-C6F1-4D72-9572-3925675E78A5 / data /容器/数据/应用程序/86A1BDD5-FAF2-486E-85A9-CF72A547C6CD/Documents/information.png,NSFilePath = / Users / macbookpro / Library / Developer / CoreSimulator / Devices / E58CA1C6-C6F1-4D72-9572-3925675E78A5 / data / Containers / Bundle / Application / EFA83E02-5F24-4BB3-B32A-7E755081A730 / AutoLayout tuts.app/information.png,NSUnderlyingError=0x7fb53251cd80 {Error Domain = NSPOSIXErrorDomain Code = 2没有这样的文件或目录}}
Error Domain=NSCocoaErrorDomain Code=4 "The file "information.png" doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/macbookpro/Library/Developer/CoreSimulator/Devices/E58CA1C6-C6F1-4D72-9572-3925675E78A5/data/Containers/Bundle/Application/EFA83E02-5F24-4BB3-B32A-7E755081A730/AutoLayout tuts.app/information.png, NSUserStringVariant=( Copy ), NSDestinationFilePath=file:///Users/macbookpro/Library/Developer/CoreSimulator/Devices/E58CA1C6-C6F1-4D72-9572-3925675E78A5/data/Containers/Data/Application/86A1BDD5-FAF2-486E-85A9-CF72A547C6CD/Documents/information.png, NSFilePath=/Users/macbookpro/Library/Developer/CoreSimulator/Devices/E58CA1C6-C6F1-4D72-9572-3925675E78A5/data/Containers/Bundle/Application/EFA83E02-5F24-4BB3-B32A-7E755081A730/AutoLayout tuts.app/information.png, NSUnderlyingError=0x7fb53251cd80 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
根据 fileManager.fileExistsAtPath()
,该文件确实存在。我在做什么错了?
According to the fileManager.fileExistsAtPath()
the file does indeed exist. What am I doing wrong?
推荐答案
问题是这一行:
let fullDestPathString = String(fullDestPath)
应该是:
let fullDestPathString = fullDestPath.path
查看错误。问题是目的地。注意 file:///
。您的代码未正确将URL转换为文件路径。您需要使用 NSURL
的 path
属性以字符串形式获取路径。
Look at the error. The problem is the destination. Notice the file:///
. Your code is not properly converting the URL to a file path. You need to use the path
property of NSURL
to get the path as a string.
在所有调试和检查中,您从未验证过 fullDestPathString
的值。
In all of your debugging and checking, you never verified the value of fullDestPathString
.
这篇关于无法将文件从捆绑包复制到iOS中的文档目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!