RxAlmofire将参数传递给requestJSON [:]

时间:2019-05-16 14:36:23

标签: ios swift4 rx-swift reactive rxalamofire

以下是 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方法

1 个答案:

答案 0 :(得分:1)

您需要从URL构建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的要求。