【问题标题】:Extra argument 'method' in call Alamofire swift 4调用 Alamofire swift 4 中的额外参数“方法”
【发布时间】:2018-10-22 18:36:29
【问题描述】:

我正在尝试从设备向设备发送通知,我之前在我的其他应用程序上做过此操作,但由于某种原因我收到此错误消息

调用中的额外参数“方法”

在 alamofire.request 函数中的方法。我已经在网上搜索了几个小时,并且尝试了很多解决方案,但是没有任何对我有用的信息我得到了相同的错误消息。

func setUpPushNotification(fromDevice: String) {


    let title = ""
    let body = "You got a friend request"
    let toDeviceID = fromDevice
    var headers:HTTPHeaders = HTTPHeaders()
    let Method = Alamofire.HTTPMethod.post

    headers = ["Content-Type":"application/json","Authorization":"key=\(AppDelegate.SERVERKEY)"]

    let notification = ["to":"\(toDeviceID)","notification":["body":body,"title":title,"badge":1,"sound":"default"]] as [String : Any]



    Alamofire.request(AppDelegate.NOTIFICATION_URL as URLConvertible,
                      method: Method,
                      parameters: notification,
                      encoding: JSONEncoding.default,
                      headers: headers).responseJSON { (response) in
        print(response)
    }



}

感谢您的宝贵时间。 :)

【问题讨论】:

  • 你能提供你正在尝试使用的 alamofire 版本吗?
  • @harsh 我正在使用 Alamofire 4.7.3 版

标签: swift swift4 alamofire http-method


【解决方案1】:

AppDelegate 中的 AppDelegate.NOTIFICATION_URL 变量有问题。

因为当我删除密钥时,这段代码开始工作。希望这会帮助您找到解决方案。

    func setUpPushNotification(fromDevice: String) {

    let title = ""
    let url = "something"
    let body = "You got a friend request"
    let toDeviceID = fromDevice
    var headers:HTTPHeaders = HTTPHeaders()
    let Method = Alamofire.HTTPMethod.post

    headers = ["Content-Type":"application/json","Authorization":"key=1236"]

    let notification = ["to":"\(toDeviceID)","notification":["body":body,"title":title,"badge":1,"sound":"default"]] as [String : Any]

    Alamofire.request(url as URLConvertible , method: Method, parameters: notification, encoding: JSONEncoding.default, headers: headers)
}

【讨论】:

  • 这行得通,我不知道为什么,但我猜有一些代码具有相同的名称,所以我只是更改了 URL 上的 var 名称。谢谢楼主:)
【解决方案2】:

appDelegate 是问题所在。 当我更换使用它的 2 val 时,它可以解决问题:

func setUpPushNotification(fromDevice: String) {


    let title = ""
    let body = "You got a friend request"
    let toDeviceID = fromDevice
    var headers:HTTPHeaders = HTTPHeaders()
    let Method = Alamofire.HTTPMethod.post

    headers = ["Content-Type":"application/json","Authorization":"key="]

    let notification = ["to":"\(toDeviceID)","notification":["body":body,"title":title,"badge":1,"sound":"default"]] as [String : Any]


    Alamofire.request("www...",
                      method: Method,
                      parameters: notification,
                      encoding: JSONEncoding.default,
                      headers: headers).responseJSON { (response) in
                        print(response)
    }
}

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 2017-06-26
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2018-07-03
    • 2018-08-26
    • 2017-01-22
    相关资源
    最近更新 更多