【问题标题】:I am getting a nil value even if i give a url variable a value, so i want to know what am i doing wrong?即使我给 url 变量一个值,我也得到一个 nil 值,所以我想知道我做错了什么?
【发布时间】:2020-11-06 11:22:27
【问题描述】:

我刚刚创建了一个应用程序,它只是从 url 下载 pdf 文件,将其保存到 cachesDirectory,然后使用 SwiftUI 在 pdfView 中显示它,请参阅我在 body 属性中的 cmets 以检查我做错了什么

    struct ContentView: View {
        
        @State var documentURL: URL?
        @State var ispresented: Bool = false

    func downloadFile(completion: @escaping (URL)->Void) {
        guard let url = URL(string: "https://www.tutorialspoint.com/swift/swift_tutorial.pdf") else { return }
       let task =  URLSession.shared.downloadTask(with: url) { (localURL, response, error) in
            if error == nil {
                let docsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
                let destinationPath = docsPath.appendingPathComponent(url.lastPathComponent)
                try? FileManager.default.removeItem(at: destinationPath)
                do {
                    try FileManager.default.copyItem(at: localURL!, to: destinationPath)
                    DispatchQueue.main.async { completion(destinationPath) }
                    
                } catch {
                    print("Error copying:\(error)")
                }
            }
        }
        task.resume()
    }
        
        var body: some View {
            VStack {
                Button(action: {
                    downloadFile { localUrl in
                        print("Closure url:\(localUrl)")

// here i am getting valid localUrl means i can see and open the downloaded file in finder when i follow the url in HardDisk 

                        self.documentUrl = localUrl
                        UserDefaults.standard.set(localUrl, forKey: "url")
                        }
                    }, label: {
                        Text("Start Downloading")
                    })
                    Button("Open PDFView") {
                        ispresented.toggle()
                    }
                }
                .sheet(isPresented: $ispresented, content: {
                    PDFKitRepresentableView(url: documentURL!)

// here i am getting error that documentURL is nil

                    
                })
     }

那么,我在这里做错了什么?提前致谢。

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    这似乎是由 downloadFile 进行异步调用引起的问题,我不确定它为什么不起作用,但我设法让它与下面的解决方案一起工作,我跳过了完成处理程序并分配了documentsUrl URLSession 闭包内的属性

    所以do子句改为

    do {
        try FileManager.default.copyItem(at: localURL!, to: destinationPath)
        documentUrl = destinationPath
    }
    

    为了使 UI 更清晰,我在第二个按钮上添加了一个 disabled 运算符,这样在文件下载之前你不能点击它

    Button("Open PDFView") {
        ispresented.toggle()
    }.disabled(documentUrl == nil)
    

    请注意,如果您想在那里做其他事情,当然可以保留关闭,但此解决方案不需要它

    【讨论】:

    • 我尝试了您的解决方案,但由于打开工作表时发现 documentURL 为零,它仍然崩溃,这是一个错误吗?
    • 您是否还为按钮添加了 .disabled?
    • 是的,现在它工作了,非常感谢,你能告诉我它背后的原因
    • @AdityaInamdar 不,我不知道这种行为的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    相关资源
    最近更新 更多