找不到我的档案

时间:2015-06-18 03:19:28

标签: ios xcode swift nsfilemanager

我已将文件复制到项目中,我不熟悉swift编码,但我认为该文件会部署到我的设备中,并包含项目中的所有资源文件。这是我的代码无法从软件包中找到文件或副本

  if let dirs = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .AllDomainsMask, true) as ? [String] {
      let dir = dirs[0] //documents directory
      let path = dir.stringByAppendingPathComponent(file)
      var checkValidation = NSFileManager.defaultManager()

      if checkValidation.fileExistsAtPath(path) {
        println("FILE AVAILABLE")
      } else {
        println("FILE NOT AVAILABLE")
        // Copy the file from the Bundle and write it to the Device:
        let pathToBundledDB = NSBundle.mainBundle().pathForResource("pcQuestionDBA", ofType: "txt")
        var error: NSError ?
        if NSFileManager.defaultManager().copyItemAtPath(pathToBundledDB!, toPath: dir, error: & error) {

        }

我知道我必须做一些根本错误的事情,任何建议都会得到真正的赞赏 - 谢谢

更新: 我已将文件复制到项目文件夹中,但是,我删除了该文件并尝试重新复制它,并收到一条错误消息,指出该文件已存在于该文件夹中。我想我已经把Xcode与我试图修复我的bug并找到文件的多个动作和变化混淆了。我重命名了该文件并从头开始,现在一切正常。非常感谢您的快速回复,最终让我得到了解决方案 - 欢呼声

1 个答案:

答案 0 :(得分:1)

问题在于您应该将路径传递给该文件,但是您将路径传递给该目录。

试试这样:

NSFileManager.defaultManager().copyItemAtPath(pathToBundledDB!, toPath: path, error: &error)