0

以下是 RxAlamofire 提供的用于发出 requestJSON 请求的方法 没有传递参数的方法 [String : String]

RxAlamofire.requestJSON(.get, url)
        .subscribe(onNext: { [weak self] (r, json) in
            if let jsonResult = JSON(json) as? JSON {
                if let foodMenuResult = MenuResult(jsonResult) as? MenuResult {
                    self?.delegate?.showMenu(menuResult: foodMenuResult)
                }
            }

            }, onError: {  [weak self] (error) in
                self?.delegate?.onError()
            },onCompleted: {})
        .disposed(by: disposeBag)

如何将参数传递给RxAlamofire requestJSON 方法

4

1 回答 1

1

您需要构造URLfrom URLComponents

var components = URLComponents(string: "https://www.example.com/path")!
components.queryItems = [
    .init(name: "param1", value: "value1"),
    .init(name: "param2", value: "value2"),
]

let url = components.url!

url以上将

https://www.example.com/path?param1=value1&param2=value2

上面的使用!应该没有问题,因为您传递的数据应该是有效的。请参阅这些方法的文档以评估正确nil处理的要求。

于 2019-05-16T17:43:46.057 回答