Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift FileManager error when copying file from Bundle

I'm trying to copy a file from my App's Bundle to the device and I'm getting a strange error: cannot convert the expression type '$T5' to type 'LogicValue'

I commented the line that is causing the problem in the code below.

Here's everything:

// This function returns the path to the Documents folder:
func pathToDocsFolder() -> String {
    let pathToDocumentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

    return pathToDocumentsFolder.stringByAppendingPathComponent("/moviesDataBase.sqlite")
}


override func viewDidLoad() {
    super.viewDidLoad()

    let theFileManager = NSFileManager.defaultManager()

    if theFileManager.fileExistsAtPath(pathToDocsFolder()) {
        println("File Found!")
        // And then open the DB File
    }
    else {
        // Copy the file from the Bundle and write it to the Device:
        let pathToBundledDB = NSBundle.mainBundle().pathForResource("moviesDB", ofType: "sqlite")
        let pathToDevice = pathToDocsFolder()

        let error:NSError?

        // Here is where I get the error:
        if (theFileManager.copyItemAtPath(pathToBundledDB, toPath:pathToDevice, error:error)) {
            // success
        }
        else {
            // failure 
        }
    }
}

The App won't even compile right now. The issue seems to be specifically with the copyItemAtPath call - which is supposed to return a Bool.

I'd appreciate any insights.

like image 340
sirab333 Avatar asked Oct 01 '22 07:10

sirab333


1 Answers

There's two issues here:

  1. If you specify the error variable as let then it's not mutable and so you can't get an error value back.

  2. You are supposed to send a pointer to the error variable and not the variable itself. So in the line where you get the compiler error, it should be &error and not error.

like image 191
Fahim Avatar answered Oct 04 '22 15:10

Fahim