<bdo id='mLYJk'></bdo><ul id='mLYJk'></ul>
    <tfoot id='mLYJk'></tfoot>
  1. <i id='mLYJk'><tr id='mLYJk'><dt id='mLYJk'><q id='mLYJk'><span id='mLYJk'><b id='mLYJk'><form id='mLYJk'><ins id='mLYJk'></ins><ul id='mLYJk'></ul><sub id='mLYJk'></sub></form><legend id='mLYJk'></legend><bdo id='mLYJk'><pre id='mLYJk'><center id='mLYJk'></center></pre></bdo></b><th id='mLYJk'></th></span></q></dt></tr></i><div id='mLYJk'><tfoot id='mLYJk'></tfoot><dl id='mLYJk'><fieldset id='mLYJk'></fieldset></dl></div>

    <small id='mLYJk'></small><noframes id='mLYJk'>

    <legend id='mLYJk'><style id='mLYJk'><dir id='mLYJk'><q id='mLYJk'></q></dir></style></legend>

    1. 无法将文件从捆绑包复制到 iOS 中的文档目录

      时间:2024-04-15
      <i id='tEPFo'><tr id='tEPFo'><dt id='tEPFo'><q id='tEPFo'><span id='tEPFo'><b id='tEPFo'><form id='tEPFo'><ins id='tEPFo'></ins><ul id='tEPFo'></ul><sub id='tEPFo'></sub></form><legend id='tEPFo'></legend><bdo id='tEPFo'><pre id='tEPFo'><center id='tEPFo'></center></pre></bdo></b><th id='tEPFo'></th></span></q></dt></tr></i><div id='tEPFo'><tfoot id='tEPFo'></tfoot><dl id='tEPFo'><fieldset id='tEPFo'></fieldset></dl></div>

        <small id='tEPFo'></small><noframes id='tEPFo'>

      1. <tfoot id='tEPFo'></tfoot>

            <tbody id='tEPFo'></tbody>
          <legend id='tEPFo'><style id='tEPFo'><dir id='tEPFo'><q id='tEPFo'></q></dir></style></legend>

                <bdo id='tEPFo'></bdo><ul id='tEPFo'></ul>
              • 本文介绍了无法将文件从捆绑包复制到 iOS 中的文档目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用以下代码将文件从我的包复制到 iOS 中的文档目录.

                让 bundlePath = NSBundle.mainBundle().pathForResource("information", ofType: ".png")print(bundlePath, "
                ")//打印正确的路径让 destPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!让 fileManager = NSFileManager.defaultManager()让 fullDestPath = NSURL(fileURLWithPath: destPath).URLByAppendingPathComponent("information.png")让 fullDestPathString = String(fullDestPath)print(fileManager.fileExistsAtPath(bundlePath!))//打印 true做{尝试 fileManager.copyItemAtPath(bundlePath!, toPath: fullDestPathString)}抓住{打印(
                ")打印(错误)}

                <块引用>

                Error Domain=NSCocoaErrorDomain Code=4 "文件information.png"不存在."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=(复制), 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 "没有这样的文件或目录"}}

                根据 fileManager.fileExistsAtPath() 文件确实存在.我做错了什么?

                解决方案

                问题出在这行:

                让 fullDestPathString = String(fullDestPath)

                应该是:

                让 fullDestPathString = fullDestPath.path

                查看错误.问题是目的地.注意 file:///.您的代码未正确将 URL 转换为文件路径.您需要使用 NSURLpath 属性来获取字符串形式的路径.

                在您所有的调试和检查中,您从未验证过 fullDestPathString 的值.

                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, "
                ") //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("
                ")
                    print(error)
                }
                

                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"}}

                According to the fileManager.fileExistsAtPath() the file does indeed exist. What am I doing wrong?

                解决方案

                The problem is this line:

                let fullDestPathString = String(fullDestPath)
                

                It should be:

                let fullDestPathString = fullDestPath.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.

                In all of your debugging and checking, you never verified the value of fullDestPathString.

                这篇关于无法将文件从捆绑包复制到 iOS 中的文档目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何复制“字典"在斯威夫特? 下一篇:保留和复制的区别?

                相关文章

                1. <tfoot id='ujZGx'></tfoot>
                    <bdo id='ujZGx'></bdo><ul id='ujZGx'></ul>
                2. <small id='ujZGx'></small><noframes id='ujZGx'>

                  <i id='ujZGx'><tr id='ujZGx'><dt id='ujZGx'><q id='ujZGx'><span id='ujZGx'><b id='ujZGx'><form id='ujZGx'><ins id='ujZGx'></ins><ul id='ujZGx'></ul><sub id='ujZGx'></sub></form><legend id='ujZGx'></legend><bdo id='ujZGx'><pre id='ujZGx'><center id='ujZGx'></center></pre></bdo></b><th id='ujZGx'></th></span></q></dt></tr></i><div id='ujZGx'><tfoot id='ujZGx'></tfoot><dl id='ujZGx'><fieldset id='ujZGx'></fieldset></dl></div>
                3. <legend id='ujZGx'><style id='ujZGx'><dir id='ujZGx'><q id='ujZGx'></q></dir></style></legend>