首页 > 解决方案 > 无法将类型“[String : String]”的值转换为预期的参数类型“HTTPHeaders?”

问题描述

我正在尝试从我的 iOS 应用程序发送邮件,Mailgun但我Alarmofire发现了这段代码Xcode会产生错误:

无法将类型“[String : String]”的值转换为预期的参数类型“HTTPHeaders?”

代码:

let parameters = [
                   "from": "sender@whatyouwant.com",
                     "to": "anyRecipient@example.com",
                "subject": "Subject of the email",
                   "text": "This is the body of the email."]
    let header = [
            "Authorization": "Basic MY-API-KEY",
            "Content-Type" : "application/x-www-form-urlencoded"]

    let url = "https://api.mailgun.net/v3/MY-DOMAIN/messages"
    Alamofire.request(url,
                   method: .post,
               parameters: parameters,
                 encoding: URLEncoding.default,
                  headers: header)
            .responseJSON { response in
                print("Response: \(response)")
                }

有什么建议么?

标签: swiftalamofiremailgun

解决方案


您需要明确设置类型。

let headers : HTTPHeaders = [
    "Authorization": "Basic MY-API-KEY",
    "Content-Type" : "application/x-www-form-urlencoded"
]

推荐阅读