swift - Swift字典通过另一个键的值设置键的值,格式为:“value_of_key1=value_of_key2”
问题描述
我正在使用 Swift 4 上的 Dictionary,它在委托中具有这种格式,然后将其发送到另一个视图控制器:
{
"search_key": "color"
"option_value": "409"
}
我的要求是以如下格式将其发送到服务器:color=409
因为它将用作请求 URL 中的参数。我试图让它像下面的代码一样,但它显示为search_key=409
data["search_key"] = itemFilter[indexPath.section].search_key.count > 0 ? itemFilter[indexPath.section].search_key : strData.search_key;
data["option_value"] = strData.option_value
dataAfterSet["search_key"] = strData.option_value //this line is when I try to set the format and it show `search_key=409`
guard let dataNew = data as? [NSObject : Any] else {return}
delegate.filterTableCellDidSelectItem(item: dataNew, indexPath: indexPath)
那么,我该如何组合它们呢?我可以做到但仍然保留字典类型,因为在委托中我将它传递给字典中的其他人?
解决方案
如果您需要在正文中传递它,我认为您需要这个:
[
"search_key": [
"color" : 405
],
"option_value": "409"
]
如果您尝试在 URL 中传递它:
let valueToPass = "color=\(405)" // replace 405 with your int value
let YOUR_URL = "https://example.com/api/" // dont forget the backslash
let url = YOUR_URL + valueToPass
推荐阅读
- c# - 使用 C# 与 Web 元素交互不起作用
- c++ - 编译器是否允许对本地 volatile 进行常量折叠?
- java - “例外:扫描仪已关闭”如何在(while..循环)内重新打开它
- c# - C# 8 异步流与 REST/RPC
- javascript - EventListener Click 对内部的 H 标记不起作用
- javascript - Axios delete call returns 405 and I tried to delete node using id but it's not deleting
- python - 过滤外键日期 django_filters
- sql - 我可以使用哪个工具导出到已经有一些数据的目标数据库
- c++ - Fast run-time vector based std::stack. Pre-allocation and multiple pop
- azure - 非法连接字符串