【问题标题】:Swift FileManager error when copying file from Bundle从Bundle复制文件时出现Swift FileManager错误
【发布时间】:2014-09-10 03:55:27
【问题描述】:

我正在尝试将文件从我的应用程序包复制到设备,但我收到了一个奇怪的错误: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 
        }
    }
}

应用程序现在甚至无法编译。这个问题似乎与copyItemAtPath 调用有关——它应该返回一个布尔值。

如果有任何见解,我将不胜感激。

【问题讨论】:

    标签: swift bundle nsfilemanager


    【解决方案1】:

    这里有两个问题:

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

    2. 您应该发送指向error 变量的指针,而不是变量本身。所以在你得到编译器错误的那一行,它应该是&error 而不是error

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 2021-04-01
      • 2015-07-16
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      相关资源
      最近更新 更多