ios - 是否可以使用 Alamofire swift 在 api 命中时将数组和字典都发送到参数中?

标签 ios swift iphone api alamofire

我的值(value)观如下。

let values = ["06786984572365", "06644857247565", "06649998782227"]
let params:[String:Any] = ["delete_wishlist_products":"1"]

是否有任何可能的方法可以在 Alamofire 中使用 Swifft 在 API 命中时将字典和数组作为不带 header 的参数发送。

在这里找到我的示例代码。

 func deleteWishlistValue(){

        let values = ["12345", "1345", "1234"]
        let params:[String:Any] = ["delete_wishlist_products":"1",
                                   "product_id":values,
                                   "customer_id":"152698"]

        Alamofire.request(Services.deleteWishlist,method: .get,parameters: params, headers:nil).responseJSON { (response) in
                            if response.result.error == nil{
                                print(response)
                            }else{
                                print(response.result.error?.localizedDescription ?? "")
                            }
        }
    }

最佳答案

这里是如何使用 almofire 在 api 中发送数组数据的示例

func getContest(arrDetails: [String],  completion: @escaping(_ reponse: ContestListResponse?, _ error: ApiError?)-> Void) {
    contestListRequest?.cancel()
    let parameters: Parameters = [
        "limit": "10",
        "details": arrDetails
        ]
let headers: HTTPHeaders = [
        "language": DBManager.getUserData()?.language ?? "eng"
    ]
    contestListRequest = Alamofire.request(AppURL.contestList, method:.post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
        .responseContestListResponse(completionHandler: { (response) in
            switch response.result {
            case .success(let result):
                print("result---",result)
                if response.response?.statusCode == 200 {
                    completion(result, nil);
                } else {
                    completion(result, ApiError(domain: .api, code: response.response?.statusCode ?? 0, msg: result.message ?? ""))
                }
            case .failure(let error):
                completion(nil, ApiError(domain: .system, code: error._code, msg: error.localizedDescription))
            }
        })
        .responseString { (response) in
            print(response.result.value ?? "NO Response")
    }
}

自行更改响应类和完成处理程序。

请注意编码:JSONEncoding.default这是您将数组或字典直接发送到参数所必需的,要求您的后端开发人员获取相应的属性。

关于ios - 是否可以使用 Alamofire swift 在 api 命中时将数组和字典都发送到参数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58927188/

相关文章:

ios - 只允许一个 View Controller 的所有方向

swift - 对 UIView 扩展的文本颜色进行动画处理

ios - 即使执行了更改图像的代码,按钮的图像也没有改变

ios - Swift 中的音频队列服务播放器不调用回调

ios - ABCreateStringWithAddressDictionary 弃用

iphone -/usr/bin/codesign 失败,退出代码为 1

ios - 代码 8 : Interface builder shows different color that set

ios - 'force' 应用程序是否可以接受新的模型版本,即使这意味着覆盖现有版本?

objective-c - 为什么我的向下钻取应用程序崩溃了?

iphone - 如何在调整 UIView 大小时设置动画