在Swift 2.0中如何将stringByAddingPercentEncodingWithAllowedCharacters()用于URL [英] How to use stringByAddingPercentEncodingWithAllowedCharacters() for a URL in Swift 2.0

查看:304
本文介绍了在Swift 2.0中如何将stringByAddingPercentEncodingWithAllowedCharacters()用于URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift 1.2中使用了这个

I was using this, in Swift 1.2

let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

这现在给我一个警告,要求我使用

This now gives me a warning asking me to use

stringByAddingPercentEncodingWithAllowedCharacters

我需要使用 NSCharacterSet 作为参数,但是有那么多,我无法确定是什么能给我与以前使用的方法相同的结果.

I need to use a NSCharacterSet as an argument, but there are so many and I cannot determine what one will give me the same outcome as the previously used method.

我要使用的示例网址如下

http://www.mapquestapi.com/geocoding/v1/batch?key=YOUR_KEY_HERE&callback=renderBatch&location=Pottsville,PA&location=Red Lion&location=19036&location=1090 N Charlotte St, Lancaster, PA

用于编码的URL字符集似乎包含修剪 网址.即

The URL Character Set for encoding seems to contain sets the trim my URL. i.e,

URL的路径部分是紧随在 主机组件(如果存在).它在查询或片段的任何地方结束 组件开始.例如,在URL中 http://www.example.com/index.php?key1=value1 ,路径组件为 /index.php.

The path component of a URL is the component immediately following the host component (if present). It ends wherever the query or fragment component begins. For example, in the URL http://www.example.com/index.php?key1=value1, the path component is /index.php.

但是我不想修剪它的任何方面. 当我使用String时,例如myurlstring,它将失败.

However I don't want to trim any aspect of it. When I used my String, for example myurlstring it would fail.

但是使用以下内容时,没有任何问题.它用一些魔术对字符串进行编码,我可以得到我的URL数据.

But when used the following, then there were no issues. It encoded the string with some magic and I could get my URL data.

let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

照常

使用给定的编码返回String的表示形式,以 确定将String转换为a所需的转义百分比 合法的网址字符串

Returns a representation of the String using a given encoding to determine the percent escapes necessary to convert the String into a legal URL string

谢谢

推荐答案

对于给定的URL字符串,等效于

For the given URL string the equivalent to

let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

是字符集URLQueryAllowedCharacterSet

let urlwithPercentEscapes = myurlstring.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())

快捷键3:

let urlwithPercentEscapes = myurlstring.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)

它对URL字符串中问号后的所有内容进行编码.

It encodes everything after the question mark in the URL string.

由于方法stringByAddingPercentEncodingWithAllowedCharacters可以返回nil,因此请使用Leo Dabus的答案中建议的可选绑定.

Since the method stringByAddingPercentEncodingWithAllowedCharacters can return nil, use optional bindings as suggested in the answer of Leo Dabus.

这篇关于在Swift 2.0中如何将stringByAddingPercentEncodingWithAllowedCharacters()用于URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆