从Bundle复制文件时Swift FileManager错误

sir*_*333 5 bundle nsfilemanager swift

我正在尝试将我的App的Bundle中的文件复制到设备上,我收到一个奇怪的错误: cannot convert the expression type '$T5' to type 'LogicValue'

我评论了下面代码中导致问题的那一行.

这是一切:

// 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 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该应用程序甚至不会立即编译.这个问题似乎特别与 copyItemAtPath调用有关 - 这应该是一个Bool.

我很欣赏任何见解.

Fah*_*him 4

这里有两个问题:

  1. 如果您将error变量指定为,let则它是不可变的,因此您无法返回错误值。

  2. 您应该发送指向error变量的指针而不是变量本身。因此,在出现编译器错误的行中,它应该是&error而不是error