Swift 3.1 迁移 Dispatch.main.async 对成员的引用不明确 'async(execute:)'
Swift 3.1 Migration Dispatch.main.async ambiguous reference to member 'async(execute:)'
大家好,我最近将我的 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 行收到 "Ambiguous reference to member 'async(execute:)'" 错误。你们能知道这是怎么回事吗?
问题不在于 DispatchQueue.main.async
,而在于 Alamofire 的 uploadProgress 语法,将其替换为以下块,问题得到解决:
uploads.uploadProgress { (progress: Progress) in
DispatchQueue.main.async {
progressview.setProgress(progress.fractionCompleted, animated: true)
}
}
大家好,我最近将我的 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 行收到 "Ambiguous reference to member 'async(execute:)'" 错误。你们能知道这是怎么回事吗?
问题不在于 DispatchQueue.main.async
,而在于 Alamofire 的 uploadProgress 语法,将其替换为以下块,问题得到解决:
uploads.uploadProgress { (progress: Progress) in
DispatchQueue.main.async {
progressview.setProgress(progress.fractionCompleted, animated: true)
}
}