Cannot find my file

175 views Asked by At

I have copied the file to the project and I am new to coding in swift, but I assumed that the file would deploy to my device with all the resource files in the project. this is my code which fails to find the file or copy from the bundle

  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) {

        }

I know I must be doing something fundamentally wrong and any advice would be truly appreciated - thanks

An Update: I had copied the file into the project folder, however, I deleted the file and tried to copy it in anew, and I received an error stating that the file already existed in the folder. I think I had confused Xcode with the multiple moves and variations I tried in an attempt to fix my bug and find the file. I renamed the file and started from scratch and it all works well now. Thanks so much for your quick replies, it led me to the solution in the end - cheers

1

There are 1 answers

0
Leo Dabus On BEST ANSWER

The problem there is that you should be passing a path to the file but you passed the path to the directory.

Try like this:

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