由于“|”无法创建 NSURL 对象url 字符串中的符号

Can not create NSURL object because of "|" symbol in url string

我需要从 url 字符串创建一个 NSURL 对象,例如 this。它包含 | 符号。问题是 NSURL 构造函数总是 returns nil,因为 | 符号。我怎样才能创建这个对象?

我认为是小写 l,而不是竖线 | 字符。

但是,您可以使用 stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQue‌​ryAllowedCharacterSet() 来转义 URL:

中的无效字符
url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQue‌​ryAllowedCharacterSet())

--

let url = "https://static-maps.yandex.ru/1.x/?l=map&pt=55,1583062965,61,3948104504,pm2rdm&size=600,300"    
let escapedURL = url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQue‌​ryAllowedCharacterSet())
print(NSURL(string: escapedURL))