我已阅读有关此问题this和this的所有问题。我有以下代码
let fullURL = God.getFullURL(apiURL: self.apiUrl)
if (getOrPost == God.POST) {
Alamofire.request(fullURL, method: .POST, AnyObject: self.postData?, encoding:.JSONEncoding.default, headers: nil).responseJSON{ response in
self.responseData = response.result.value
}
} else if (getOrPost == God.GET) {
Alamofire.request(fullURL, method : .GET, Parameters: getData, encoding:.JSONEncoding.default, headers: nil).responseJSON{ response in
self.responseData = response.result.value
}
}
My Swift和Xcode版本
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
Version 8.2.1 (8C1002)
我的pod文件是
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target 'Buseeta' do
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'master'
end
pod 'AlamofireObjectMapper', '~> 4.0'
pod 'SwiftyJSON'
我在两个Alamofire请求行上都出现Extra argument 'method' in call
错误。
不要仔细检查,不要将此问题标记为重复。我已按照重复的问题完全修复了代码。
编辑1
我在.POST
和.GET
let fullURL = God.getFullURL(apiURL: self.apiUrl)
if (getOrPost == God.POST) {
Alamofire.request(fullURL, method: .POST, AnyObject: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
self.responseData = response.result.value
}
} else if (getOrPost == God.GET) {
Alamofire.request(fullURL, method : .GET, Parameters: getData?, encoding:.JSONEncoding.default).responseJSON{ response in
self.responseData = response.result.value
}
}
编辑2
if (getOrPost == God.POST) {
Alamofire.request(fullURL, method: .post, parameters: self.postData?, encoding:.JSONEncoding.default).responseJSON{ response in
self.responseData = response.result.value
}
} else if (getOrPost == God.GET) {
Alamofire.request(fullURL, method : .get, parameters: getData?, encoding:.JSONEncoding.default).responseJSON{ response in
self.responseData = response.result.value
}
}
编辑3
我替换为method : HTTPMethod.get,
仍然没有变化。同样的问题。
答案 0 :(得分:1)
您需要使用其他功能上传数据:
func upload(
_ data: Data,
to url: URLConvertible,
method: HTTPMethod = .post,
headers: HTTPHeaders? = nil)
-> UploadRequest
GET请求的参数必须是[String:Any]
类型答案 1 :(得分:0)
答案在您的链接中,这是方法请求的新定义:
(Connection's Name): Sheet'sName|1st Cell of Range
Connection's ODBC Command Text
(Connection's Name): Sheet'sName|1st Cell of 1st Range // Sheet'sName|1st Cell of 2nd Range
答案 2 :(得分:0)
我试过的是
var postData : Parameters?
var getData : Parameters?
在init中,我将postData初始化为
self.postData = try! JSONSerialization.data(withJSONObject: postData, options: JSONSerialization.WritingOptions.prettyPrinted)
和
if (getOrPost == God.POST) {
Alamofire.request(fullURL, method: HTTPMethod.post, parameters: postData as Parameters?, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
self.responseData = response.result.value
}
} else if (getOrPost == God.GET) {
Alamofire.request(fullURL, method: HTTPMethod.get, parameters: getData, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
self.responseData = response.result.value
}
}
修改强>
这编译没有错误:)。我尝试了@ cosmicman66
的upload
推荐
if (getOrPost == God.POST) {
Alamofire.upload(postData!, to: fullURL, headers:nil).responseJSON { response in
self.responseData = response.result.value
}
} else if (getOrPost == God.GET) {
Alamofire.request(fullURL, method: HTTPMethod.get, parameters: getData, encoding: JSONEncoding.default, headers: nil).responseJSON{ response in
self.responseData = response.result.value
}
}
修改强>
这不起作用。请求没有达到。我在某处读到上传不应该用于POST。
答案 3 :(得分:0)
当我为var payload = [String:String]()
payload["message"] = message.text!
payload["dialog_id"] = self.dialog.id!
do {
let data = try JSONSerialization.data(withJSONObject: payload, options: .prettyPrinted)
let message = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
var opponentIDs: [String] = []
for userId in self.dialog.occupantIDs! {
// Discard currently logged in user
if userId.uintValue != _user.id {
opponentIDs.append(String(describing: userId))
}
}
let event = QBMEvent()
event.message = message
event.usersIDs = opponentIDs.joined(separator: ",")
event.notificationType = QBMNotificationType.push
event.type = QBMEventType.oneShot
QBRequest.createEvent(event, successBlock: { (response, arrEvents) in
kprint(items: event.name ?? "")
}, errorBlock: { (errRes) in
kprint(items: errRes.error?.description ?? "")
})
} catch {
print(error.localizedDescription)
}
传递NSDictionary
时出现此错误,更不用说错误消息具有误导性。
根据文档,请求的参数类型为[String:Any]。我通过这样做解决了我的问题。
我在使用Xcode 8.3.1, Alamofire 4.4.0
这是对我有用的代码:
parameters
Alamofire.request(requestUrl, method: .post, parameters: requestParameters(), encoding: JSONEncoding.default, headers: nil)
.responseJSON { response in
print(response)
}
是String,requestUrl
返回类型为[String:Any]的swift字典。它是requestParameters()
,而不是JSONEncoding.default
请参阅GitHub上的这个主题以获得澄清:
答案 4 :(得分:-1)
我面临着同样的问题,但是通过更改参数类型为[String:any]解决了我的问题
let parameters : [String:any] = ["key":value]