IOS可以复制超过300MB的本地文件吗?

Can IOS copy local file over 300MB?

提问人:P.Coder 提问时间:10/18/2020 更新时间:10/19/2020 访问量:56

问:

问好全世界每一位了不起的开发者!

我试图做的是将数据库文件从主应用程序包复制到IOS文档文件夹,代码看起来不错,但总是无法复制文件,目标文件总是零字节! 所以,出错的是下面的代码!

斯威夫特 5

    private func moveDbFile() {
        
        let fileManager = FileManager.default
        let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
        print(documentsDirectory)
        
        guard let sourcePath = Bundle.main.path(forResource: "database", ofType: "db") else {
            return
        }

        if fileManager.fileExists(atPath: sourcePath) {
            let sourceUrl = URL(fileURLWithPath: sourcePath)
            let destination = documentsDirectory.appendingPathComponent("database.db", isDirectory: false)
            try? fileManager.copyItem(at: sourceUrl, to: destination)

            if fileManager.fileExists(atPath: destination.path) {
                print("file copied")
            } else {
                print("file copy failed")
            }
        }
    }
iOS 的 swift iphone xcode sqlite

评论

4赞 OOPer 10/18/2020
你能用 -- 而不是 ?dotrycatchtry?
0赞 P.Coder 10/18/2020
据我所知,没有错误
0赞 bhr 10/18/2020
一旦实现,catch 块中应该会抛出一个错误。详情如下:developer.apple.com/documentation/foundation/filemanager/...
0赞 El Tomato 10/18/2020
“目标文件始终为零字节” 何时以及如何计算文件大小?您如何知道应用程序何时完成文件大小的计算?
0赞 P.Coder 10/18/2020
@ElTomato您可以通过多种方式检查文件大小,简单的方法是访问IOS模拟器中的文件夹.document

答:

0赞 P.Coder 10/19/2020 #1

通过将数据库文件添加到项目目标成员身份来修复

评论

1赞 DonMag 10/20/2020
这就是为什么你应该使用 .do / try / catch