0

我正在尝试转换我的上传图像程序。但停滞不前发生了几个问题。
我想要这个问题的任何建议。
下面是以前使用 Alamofire 3 的代码。

    Alamofire.upload(
        .POST,
        "https://uploadURL",
        multipartFormData: { multipartFormData in
            multipartFormData.appendBodyPart(data: imageData!, name: "image", fileName: nowString + "To" + receiverString! + ".jpg", mimeType: "image/jpg")
            for (key, value) in parameters {
                multipartFormData.appendBodyPart(data: value!.data(using: String.Encoding.utf8)!, name: key)
            }
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseString(completionHandler: { (response) in
                    debugPrint(response)
                })

            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

下面是为 Alamofire 4.0.1 和 swift 3 编写的当前代码。(不完整)

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(data: imageData!, name: "image", fileName: nowString + "To" + receiverString! + ".jpg", mimeType: "image/jpg")
            for (key, value) in parameters {
                multipartFormData.append(data: value!.data(using: String.Encoding.utf8)!, name: key)
            }
        },to:"https://uploadURL",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseString(completionHandler: { (response) in
                    debugPrint(response)
                })

            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

围绕“multipartFormData”生成错误消息。

'append' 的重载存在这些部分匹配的参数列表:(Data, withName: String, fileName: String, mimeType: String), (URL, withName: String, fileName: String, mimeType: String)

将“Any”转换为“AnyObject”或使用“as!” 强制向下转换为更具体的类型以访问成员

我正在寻找这个问题的解决方案......

4

1 回答 1

0

append 方法签名发生了一些变化。您正在使用以下内容:

append(data:name:fileName:mimeType:)

他们将name参数重命名withName为符合 Swift 3 API 设计指南。所以你应该使用:

append(_:withName:fileName:mimeType:)

如下所示修改您的代码,它应该可以正常工作。

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(imageData!, withName: "image", fileName: nowString + "To" + receiverString! + ".jpg", mimeType: "image/jpg")
        for (key, value) in parameters {
            multipartFormData.append(data: value!.data(using: String.Encoding.utf8)!, name: key)
        }
    },to:"https://uploadURL",
      encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseString(completionHandler: { (response) in
                debugPrint(response)
            })

        case .failure(let encodingError):
            print(encodingError)
        }
    }
)

注意:我唯一改变的是name参数,所以它是withName。我删除了data标签。

于 2016-10-26T07:21:29.447 回答