对成员 'upload(_:_:headers:file:)' 的模糊引用

Posted

技术标签:

【中文标题】对成员 \'upload(_:_:headers:file:)\' 的模糊引用【英文标题】:Ambiguous reference to member 'upload(_:_:headers:file:)'对成员 'upload(_:_:headers:file:)' 的模糊引用 【发布时间】:2017-02-17 15:34:40 【问题描述】:

下面的代码在 Swift 3 中完美地工作以将图像文件作为多部分上传。但是,我无法在 Swift 2.2 中获得任何类似的工作。如果我尝试在 Swift 2.2 中使用它,我会收到消息 Ambiguous reference to member 'upload(_:_:headers:file:)'

有没有办法在 Swift 2.2 中完成同样的事情?我发现了几个相关的问题,但只找到了适用于 Swift 3 的解决方案。

func submitFile(entryId: Int, entryDetailValue: String, fieldId: Int, fieldType: String) 

    let parameters = [
        "entryId": "\(entryId)",
        "entryDetail": entryDetailValue,
        "fieldId": "\(fieldId)",
        "type": fieldType
    ]

    print(parameters)

    Alamofire.upload(multipartFormData:  (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(self.imageView.image!, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
        for (key, value) in parameters 
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        
        , to:"<my endpoint url>")
     (result) in
        switch result 
        case .success(let upload, _, _):

            upload.uploadProgress(closure:  (progress) in
                //Print progress
                print(progress)
            )

            upload.responseData  response in
                print(response.result)
            

        case .failure(let error):
            print(error)
        
    


【问题讨论】:

【参考方案1】:

在 swift 3 中试试这个

func uploadImageWithParameter()

    let id = String(describing: userDefaults.value(forKey: USER_LOGIN_ID)!)

    var parameters = [String:String]()
    parameters = ["title":headingTextfield.text!,
                  "id":id,
                  "description":descriptionTextview.text,
                  "author":authorTextfield.text!,
                  "url": urlTextfield.text!]

    Alamofire.upload(multipartFormData:  (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(self.capturedImage, 0.5)!, withName: "pic", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
        for (key, value) in parameters 
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        

    , to:"your url")
     (result) in
        switch result 
        case .success(let upload, _, _):

            upload.uploadProgress(closure:  (Progress) in
                print("Upload Progress: \(Progress.fractionCompleted)")
            )

            upload.responseJSON  response in
                //self.delegate?.showSuccessAlert()
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization
                //                        self.showSuccesAlert()
                //self.removeImage("frame", fileExtension: "txt")
                if let JSON = response.result.value 
                    print("JSON: \(JSON)")
                
            

        case .failure(let encodingError):
            //self.delegate?.showFailAlert()
            print(encodingError)
        

    

【讨论】:

以上是关于对成员 'upload(_:_:headers:file:)' 的模糊引用的主要内容,如果未能解决你的问题,请参考以下文章

RxAlamofire:对成员 'json(_:_:parameters:encoding:headers:)' 的模糊引用

对成员 '(_:numberOfRowsInSection:)' 的模糊引用

对成员 'collectionView(_:layout:minimumLineSpacingForSectionAt:)' 的模糊引用

使用 CloudKit 保存尝试对成员“保存(_:completionHandler :)”的模糊引用

重新加载集合视图数据时对成员 'collectionView(_:numberOfItemsInSection:)' 的模糊引用

Alamofire(Swift 3):对成员“上传(..”)的模糊引用