从Bundle复制文件时Swift FileManager错误

时间:2021-06-05 23:54:36

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'

我正在尝试将我的App的Bundle中的文件复制到设备上,我收到一个奇怪的错误:无法将表达式'$ T5'转换为'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.

该应用程序甚至不会立即编译。这个问题似乎特别针对copyItemAtPath调用 - 它应该返回一个Bool。

I'd appreciate any insights.

我很欣赏任何见解。

1 个解决方案

#1


4  

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.

    如果您将错误变量指定为let,则它不可变,因此您无法获得错误值。

  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.

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

#1


4  

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.

    如果您将错误变量指定为let,则它不可变,因此您无法获得错误值。

  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.

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