swift - 无法将类型“[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)")
}
有什么建议么?
解决方案
您需要明确设置类型。
let headers : HTTPHeaders = [
"Authorization": "Basic MY-API-KEY",
"Content-Type" : "application/x-www-form-urlencoded"
]
推荐阅读
- c++ - Code::Blocks 说在 linux 上找不到 icon.res
- excel - 如何使用 VBA 中的矩阵和循环查找并绘制具有相同内部单元格颜色的表的所有等值?
- javascript - JavaScript Split on Any Number of a Character
- java - My application getting crashes even though there is no error while building the app
- c# - 在匹配 ID 上添加来自不同列表的属性
- c++ - Python Numpy methods correspond to C++ Eigen make crash
- excel - 使用 MSXML2.xmlHttp 将特定的“src”地址内嵌
- annotations - 在 PHPUnit 中模拟 docblock 注释
- python - 如何使用 pandas 交叉表汇总年度犯罪数量?
- material-ui - 如何使用 Material UI Grid 在 React Admin 中组织 Show 布局