在Alamofire 4.0中无法使用类型为((String,withName:String))的参数列表调用'append' [英] Cannot invoke 'append' with an argument list of type '(String, withName: String)' in Alamofire 4.0
问题描述
我正在使用Alamofire 4.0通过设备/相机选择或录制视频后将视频上传到服务器,但是当我尝试使用append调用上传功能时,在所有append语句中都出现了此错误,这是怎么回事
I'm using Alamofire 4.0 to upload video to server after select or record it through device/camera but when I'm trying to call upload function with append , this error appeared to me in all append statement , what is the wrong in my code.
第二个问题是我是否要在上传过程中显示进度和进度百分比,如何通过Alamofire做到这一点。
the second my question about if I want to display progress with percentage of progress during upload how I can make that through Alamofire.
谢谢:)
阅读选定/录制的视频的网址后我的代码
My code after reading url of selected/recorded video
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType:AnyObject? = info[UIImagePickerControllerMediaType] as AnyObject?
if let type:AnyObject = mediaType {
if type is String {
let stringType = type as! String
if stringType == kUTTypeMovie as String {
let urlOfVideo = info[UIImagePickerControllerMediaURL] as? NSURL
if let url = urlOfVideo {
// 2
print(url)
let URL = try! URLRequest(url: "myurl", method: .post, headers: ["Authorization": "auth_token"])
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(url, withName: "videoFile", fileName: "filename", mimeType: "mov")
multipartFormData.append("video", withName: "load")
multipartFormData.append("record", withName: "type")
}, with: URL, encodingCompletion: { (result) in
// code
})
}
}
}
}
picker.dismiss(animated: true, completion: nil)
}
推荐答案
关于错误消息:
无法使用类型为'
(String的参数列表)调用'
'追加
' ,withName:String)
Cannot invoke '
append
' with an argument list of type '(String, withName: String)
'
如果我们看一下 MultipartFormData的
的Alamofire: append
方法
If we take a look at the append
methods of MultipartFormData
of Alamofire:
- Alamofire/Source/MultipartFormData.swift
我们注意到,没有 append(...)
方法允许类型为 String
的第一个参数,这就是您的意思,但是,尝试在 Alamofire.upload
的 multipartFormData
闭包中追加时使用。
We note that no append(...)
method allows a first argument of type String
, which is what you, however, attempt to use when appending in your multipartFormData
closure of Alamofire.upload
.
multipartFormData.append("video", withName: "load")
multipartFormData.append("record", withName: "type")
我相信您正在尝试使用下列方法:
I believe you're attempting to use the following method:
public func append(_ data: Data, withName name: String) { ... }
在这种情况下,您需要将字符串编码为Swift类型的 Data
,例如如下:
In which case you need to encode your string into the Swift type Data
, e.g. as follows:
multipartFormData.append("video".data(using: .utf8)!, withName: "load")
multipartFormData.append("record".data(using: .utf8)!, withName: "type")
至于您的通话:
As for your call:
multipartFormData.append(url, withName: "videoFile", fileName: "filename", mimeType: "mov")
上面不变的 url
的类型为 NSURL
。在Swift 3中,您应该更喜欢使用基金会类型 URL
,该类型桥接到 NSURL
,但类型不同。我们在Alamofire 4中看到,它特别希望您尝试在上面调用的 append
函数的 URL
类型:
The immutable url
above is of of type NSURL
. In Swift 3, you should prefer using Foundation type URL
instead, which bridge to NSURL
, but is not the same type. We see in Alamofire 4 that it particularly expects the URL
type for the append
function you attempt to call above:
public func append(_ fileURL: URL, withName name: String, fileName: String, mimeType: String)
您已经注意到自己可能会使用一种解决方法通过对实例 url $ c使用
NSURL
的 absoluteURL
属性调用此方法$ c>;但这只是产生了 URL
类型的可选内容。更好的方法是从一开始就使用 URL
类型,而不是 NSURL
。
You've noted yourself that you may use a workaround to call this method by using the absoluteURL
property of NSURL
upon your instance url
; but this simply yields an optional of type URL
. A better approach would simply be using the URL
type rather than NSURL
from the start.
这篇关于在Alamofire 4.0中无法使用类型为((String,withName:String))的参数列表调用'append'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!