如何使用大括号 ([]) 制作 URLQueryItem?

Posted

技术标签:

【中文标题】如何使用大括号 ([]) 制作 URLQueryItem?【英文标题】:How can I make URLQueryItem with large bracket ([])? 【发布时间】:2021-11-16 01:50:12 【问题描述】:

我需要创建一个带有大括号的查询参数的 URL。 这是一个例子。

https://samlpe.com/detail?item_code[]=smtwGBArkp3ByfrLK3KfxdoX

我尝试使用 URLComponents 和 URLQueryItem。但 URLQueryItem 不将数组(下面的["code"])作为值。

var urlComponents = URLComponents(url: URL(string: "https://samlpe.com/detail")!,
                                                        resolvingAgainstBaseURL: false)
        urlComponents.queryItems = [URLQueryItem(name: "item_code", value: ["code"])]

有人对此有想法吗?

【问题讨论】:

@Alexander 我明白了,是的,它正在工作。谢谢!。 我把我的评论变成了一个你可以投票和接受的答案 【参考方案1】:

[] 没有特殊含义。这只是:

URLQueryItem(name: "item_code[]", value: "smtwGBArkp3ByfrLK3KfxdoX")

【讨论】:

【参考方案2】:

正如@Alexander 提到的

URLQueryItem(name: "item_code[]", value: "smtwGBArkp3ByfrLK3KfxdoX")

足够了。

【讨论】:

以上是关于如何使用大括号 ([]) 制作 URLQueryItem?的主要内容,如果未能解决你的问题,请参考以下文章