ios - Swift 3.1 迁移 Dispatch.main.async 对成员 'async(execute:)' 的模糊引用

标签 ios swift3 dispatch-async

大家好,我最近将我的 iOS 应用程序迁移到了 Swift 3.1 (Xcode 8.3.3)。我已经自己解决了大部分问题,但最后一个错误仍然困扰着我。好吧,简而言之,我使用 Alamofire 库来调用 Web 服务,并通过以下几种方法使用 Dispatch Sync:

class func createVideoActivity(_ type: Int, permission: Int, message: String, video: URL, progressview:UIProgressView , completion: @escaping (_ type: ResponseType , _ response : Int, _ message: String) -> Void) {

        let user_id = CFunctions.getSession("id")

        var serviceURL = baseURL + "&task=createActivity&user_id=\(user_id)&type=\(type)&permission=\(permission)"

        serviceURL = serviceURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
        let url = URL(string: serviceURL)
        let authHeader = ["":""]

        let mimetype = "video/mov"

        var movieData: Data?
        do {
            movieData = try Data(contentsOf: URL(fileURLWithPath: (video.relativePath)), options: NSData.ReadingOptions.alwaysMapped)
        } catch _ {
            movieData = nil
            return
        }
        let filename = "upload.mov"

        upload(
            multipartFormData:{ multipartFormData in
                multipartFormData.append(movieData!, withName: "filedata",fileName: filename,mimeType: mimetype)
                multipartFormData.append(message.data(using: String.Encoding.utf8)!, withName: "message")
            },
            to: url!,
            headers: authHeader,
            encodingCompletion:
            {
                encodingResult in
                switch encodingResult {
                case .success(let uploads, _, _):

                    .uploadProgress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in

                        DispatchQueue.main.async {
                            let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
                            print(percent)
                            progressview.setProgress(percent, animated: true)
                        }

                    }
                    uploads.validate()
                    uploads.responseJSON { serverResponse in

                        switch serverResponse.result {

                        case .success(let JSON):

                            debugPrint(JSON)

                            if (JSON as AnyObject).value(forKey: "status") as! Int == 1 {
                                completion( ResponseType.kresponseTypeSuccess,(JSON as AnyObject).value(forKey: "status") as! Int, (JSON as AnyObject).value(forKey: "response") as? String ?? "")
                            } else {
                                completion( ResponseType.kresponseTypeSuccess,(JSON as AnyObject).value(forKey: "status") as! Int, (JSON as AnyObject).value(forKey: "response") as? String ?? "")


                            }

                        case .failure(let error):

                            let dataString = String(data: serverResponse.data!, encoding: String.Encoding.utf8)
                            print("createVideoActivity Request failed with error: \(String(describing: dataString))")
                            completion(ResponseType.kResponseTypeFail, error as! Int, "Service failed")

                        }

                    }
                case .failure(let encodingError):
                   print(encodingError)
                }
            }
        ) // upload - end
    }

我在 Dispatch.main.sync 行上收到“对成员‘async(execute:)’的引用不明确”错误。你们能找出发生了什么事吗?

最佳答案

问题不在于 DispatchQueue.main.async,而在于 Alamofire 的 uploadProgress 语法,将其替换为以下 block ,问题得到解决:

uploads.uploadProgress { (progress: Progress)  in
       DispatchQueue.main.async {
               progressview.setProgress(progress.fractionCompleted, animated: true)
       }
}

关于ios - Swift 3.1 迁移 Dispatch.main.async 对成员 'async(execute:)' 的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190141/

相关文章:

ios - 如何使用 NSMutableArray 进行排序?

ios - Xcode 中的证书固定

ios - 如何从中间截断 View 标题

ios - 如何以正确的方式解析 IOS SWIFT 3 中的 Google Distance Matrix API JSON

ios - secureTextEntry 不工作

ios取消dispatch_async任务

ios - 无法关闭 UIAlertView,按钮不可点击

ios - 生成 Code 39 条码

ios - tableView 滚动到底部不显示最后一部分,必须手动滚动,有人知道吗?

objective-c - 使用 dispatch_get_main_queue() 是否意味着我的代码将在主线程上?