gpt4 book ai didi

swift - 带有 Swift 3 和 alamofire 4 的 Firebase API

转载 作者:行者123 更新时间:2023-11-28 06:21:43 26 4
gpt4 key购买 nike

我正在尝试使用 Alamofire 快速使用 firebase API。我在添加 header 时遇到问题,我的请求是说我的调用中有一个额外的参数。

public static func broadcastSyncDo(localId : String){
let headers: HTTPHeaders = [
"Authorization": "key=xxx",
"Content-Type": "application/json"
]

let parameters : [String : String] = [
"to" : "/topics/"+localId,
"content_available" : "1",
"priority" : "high"
]


Alamofire.request(.post, util_Constants.FIREBASE_API, headers : headers, parameters: parameters, encoding: URLEncoding.default)
.responseJSON { response in
print("response : \(response)")
}
}

最佳答案

Swift 3.0 和 Alamofire 4 似乎有一个小错误。您必须确保所有变量的类型都完全正确,否则您将收到“调用中的额外参数”错误。以下是所有正确类型的代码应该是什么样子。

//requestString variable must be of type [String]
let requestString : String = "https://yourURLHere"

//headers variable must be of type [String: String]
let headers: [String: String] = [
"Authorization": "key=xxx",
"Content-Type": "application/json"
]

//parameters variable must be of type [String : Any]
let parameters : [String : Any] = [
"to" : "/topics/"+localId,
"content_available" : "1",
"priority" : "high"
]
//Alamofire 4 uses a different .request syntax that Alamofire 3, so make sure you have the correct version and format.
Alamofire.request(requestString, method: .post, parameters: parameters, encoding:
URLEncoding.default, headers).responseString(completionHandler:
{ responds in
//some response from JSON code here.
})

此链接 ( Alamofire Swift 3.0 Extra parameter in call) 也将有助于进一步解释您收到此错误的原因。

关于swift - 带有 Swift 3 和 alamofire 4 的 Firebase API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43232485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com