加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

从Bundle复制文件时Swift FileManager错误

发布时间:2020-12-14 02:24:50 所属栏目:百科 来源:网络整理
导读:我正在尝试将我的App的Bundle中的文件复制到设备上,我收到一个奇怪的错误:无法将表达式’$T5’转换为’LogicValue’类型 我评论了下面代码中导致问题的那一行. 这是一切: // This function returns the path to the Documents folder:func pathToDocsFolde
我正在尝试将我的App的Bundle中的文件复制到设备上,我收到一个奇怪的错误:无法将表达式’$T5’转换为’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调用 – 它应该返回一个Bool.

我很欣赏任何见解.

这里有两个问题:

>如果您将错误变量指定为let,则它不可变,因此您无法获得错误值.>您应该发送指向错误变量的指针,而不是变量本身.因此,在您收到编译器错误的行中,它应该是& error而不是error.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读