Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用String addingPercentEncoding将字符串编码为查询参数_Swift - Fatal编程技术网

Swift 使用String addingPercentEncoding将字符串编码为查询参数

Swift 使用String addingPercentEncoding将字符串编码为查询参数,swift,Swift,我有一个输入字符串“+20”,我试图将其作为url中的查询参数传递 因此,我试图通过执行以下操作对myInputString进行编码 let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 但在调试器中,字符串s1仍然显示为“+20”,而不是“%2B20” 有什么我做错了吗?正如matt所提到的+是一个合法的URL字符。如果确实需要对其进行编码,则需要创建自己的自定义urlQue

我有一个输入字符串“+20”,我试图将其作为url中的查询参数传递

因此,我试图通过执行以下操作对myInputString进行编码

let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
但在调试器中,字符串s1仍然显示为“+20”,而不是“%2B20”


有什么我做错了吗?

正如matt所提到的
+
是一个合法的URL字符。如果确实需要对其进行编码,则需要创建自己的自定义
urlQueryAllowed
,并从中减去加号:

extension CharacterSet {
    static let allowedCharacters = urlQueryAllowed.subtracting(.init(charactersIn: "+"))
}


正如matt
+
已经提到的,它是一个合法的URL字符。如果确实需要对其进行编码,则需要创建自己的自定义
urlQueryAllowed
,并从中减去加号:

extension CharacterSet {
    static let allowedCharacters = urlQueryAllowed.subtracting(.init(charactersIn: "+"))
}


+
是合法的URL字符。它不需要编码。
+
是合法的URL字符。不需要编码,谢谢。你能帮我理解为什么“+”是一个有效字符吗?当我转到时,输入
1+20
,然后输入'Url encoded',我在
Url
中得到
1%2B20
+
,这也意味着一个空格。谢谢。你能帮我理解为什么“+”是一个有效字符吗?当我转到时,输入
1+20
,然后输入'Url encoded',我在
Url
中得到
1%2B20
+
,这也意味着一个空格。