xcode 如何快速将字符串转换为 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48075421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:30:05  来源:igfitidea点击:

How to convert string to URL in swift

iosiphoneswiftxcode

提问by Akshay Phulare

I want to convert below String to URL

我想将下面的字符串转换为 URL

www.mydomain.com/key=?????

www.mydomain.com/key=?????

I tried let urlToSend = URL(string: "www.mydomain.com/key=?????")!but it returns nil.

我试过了,let urlToSend = URL(string: "www.mydomain.com/key=?????")!但它返回零。

I'm getting that '?????' keyword from textField, it could be in any local language.

我得到那个'?????' textField 中的关键字,它可以是任何本地语言。

It works fine with English but not working with local language.

它适用于英语,但不适用于当地语言。

I used let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=?????")it gives www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

我用let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=?????")它给 www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

but now I want to www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AFto www.mydomain.com/key=?????

但现在我要 www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AFwww.mydomain.com/key=?????

回答by Ahmad F

When working with URLs as strings, you should encode them to be valid as a URL; One of the most popular examples of encoding the URL is that the " " (space) would be encoded as "%20".

将 URL 作为字符串处理时,您应该将它们编码为有效的 URL;最流行的 URL 编码示例之一是“”(空格)将被编码为“%20”。

So in your case the encoded value of your url should be:

所以在你的情况下,你的 url 的编码值应该是:

www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

As you noticed the value of the keyis changed

正如您注意到的那样,值key已更改

from: "?????"

从: ”?????”

to:"%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF"

到:“%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF”

which will let the URL to be valid.

这将使 URL 有效。

How to:

如何:

you could get the above result like this:

你可以像这样得到上面的结果:

let string = "www.mydomain.com/key=?????"

if let encodedString  = string.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), let url = URL(string: encodedString) {
    print(url) // www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF
}

Note that there is optional bindingfor both the encoded string and the url for the purpose of being safe.

请注意,为了安全起见,编码字符串和 url 都有可选绑定

Decoding the URL:

解码网址:

You could also returns to the original unencoded url (decoding it) like this:

您还可以像这样返回原始未编码的 url(对其进行解码):

let decodedString = "www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF"

if let unwrappedDecodedString = decodedString.removingPercentEncoding {
    print(unwrappedDecodedString) // www.mydomain.com/key=?????
}

Again, optional binding to be safe.

再次,可选绑定是安全的。

回答by Oliver Kulpakko

You need to encode the URL.

您需要对 URL 进行编码。

let urlString = "www.mydomain.com/key=?????".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
let url = URL(string: urlString!)

let decodedUrl = urlString?.removingPercentEncoding

Keep in mind that you shouldn't force unwrap URL's and strings, use if let or guard statements.

请记住,您不应强制解开 URL 和字符串,而应使用 if let 或 guard 语句。

回答by Tejas Ardeshna

Try this thing:

试试这个:

    let strURL = "www.mydomain.com/key=?????".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    let url = URL(string: strURL!)

Or you can use:

或者你可以使用:

let strURL = "www.mydomain.com/key=?????".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
let url = URL(string: strURL!)

回答by Gereon

Instead of dealing with percent encodings explicitly, you can also build the URL piece by piece, using appendingPathComponent:

除了显式处理百分比编码之外,您还可以使用appendingPathComponent以下方法逐个构建 URL :

let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=?????")
// www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

回答by Kathiresan Murugan

Swift3.0

斯威夫特3.0

let baseUrl = "www.mydomain.com/key=?????" // base url

let encodedUrl : String! = baseUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) // remove the spaces in the url string

let typeUrl = URL(string: encodedUrl)! // convert the string into url

print(typeUrl)  // www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

For Checking purpose

用于检查目的

if let unwrappedDecodedString = encodedUrl.removingPercentEncoding {
        print(decodedString) // www.mydomain.com/key=?????
}