如何在Swift2中编写“(NSW WindowsCP1250StringEncoding)”等特殊字符 [英] How to encode special character like ’ (NSWindowsCP1250StringEncoding) in Swift2

查看:162
本文介绍了如何在Swift2中编写“(NSW WindowsCP1250StringEncoding)”等特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift2 stringByAddingPercentEscapesUsingEncoding()已被弃用,而不是 stringByAddingPercentEncodingWithAllowedCharacters()被使用。



但是如何将特殊字符编码为'%&在 swift2



例如在 iOS8 (swift1。 2)我使用以下代码编码

  NSURL(string:myurl.php?deviceName = name'phone.stringByAddingPercentEscapesUsingEncoding NSW WindowsCP1250StringEncoding)!

它工作正常,即在服务器上它正确解码。



但是在 iOS9 (Swift2.0)中,我使用以下代码

  NSURL(string:myurl.php?deviceName = name'phone.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)

它不会正确解码。

请告诉我如何在swift2.0中编码特殊字符?



编辑:
Eric D答案是正确的,但是当我编码在 stringURL 之下时,它将无法正确编码。
为什么?

  let stringURL =https://my.server.com/login.php?e=电子邮件& dn =我的iPad& d = 5& er = D550772E-34BB-4DCB-89C9-E746FAD83D24& Tz = 330
print(stringURL)
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!

let url = NSURL(string:encoded.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)!

print(url)//https%253A//my.server.com/login.php%253Fe=email&dn=my%25E2%2580%2599s%2520iPad&d=5&er = D550772E-34BB-4DCB-89C9-E746FAD83D ... 4& Tz = 330

编辑2:



如何在 swift2.0

中编码 NSW WindowsCP1250StringEncoding ? b $ b

解决方案

使用 URLPathAllowedCharacterSet()作为字符集:

  let stringURL =myurl.php?deviceName = name'phone
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL。 stringByAddingPercentEncodingWithAllowedCharacters(字符集)!
let url = NSURL(string:encoded)!

print(url)//myurl.php%3FdeviceName = name'phone

请注意,'不需要编码,它是一个有效的URL字符。


$更新:在评论中声明它仍然不起作用,因为您的编码网址被截断,并包含 ... 但实际上这可能只是NSURL截断的打印描述; NSURL对象包含的实际URL应该可以。否则会是零。您应该检查服务器端是否存在很长但正确的URL的问题。


As in Swift2 stringByAddingPercentEscapesUsingEncoding() is deprecated instead of stringByAddingPercentEncodingWithAllowedCharacters() is used.

But how to encode the especial character like ' % & in swift2

For example in iOS8(swift1.2) i used following code for encoding

NSURL(string: "myurl.php?deviceName=name’phone".stringByAddingPercentEscapesUsingEncoding(NSWindowsCP1250StringEncoding)!)

it work fine i.e. on server it decode correctly.

But in iOS9(Swift2.0) i used following code

NSURL(string: "myurl.php?deviceName=name ’phone".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)

It will not decode properly.
please tell me how i can encode special charater in swift2.0 ?

EDIT : Eric D answer is right but when i encode below stringURL it will not encode properly. Why?

let stringURL = "https://my.server.com/login.php?e=email&dn=my’s iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D24&tz=330"
        print(stringURL)
        let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
        let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!

        let url = NSURL(string: encoded.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)!

        print(url) //https%253A//my.server.com/login.php%253Fe=email&dn=my%25E2%2580%2599s%2520iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D ... 4&tz=330

EDIT 2:

How to encode NSWindowsCP1250StringEncoding in swift2.0 ?

解决方案

Use URLPathAllowedCharacterSet() as character set:

let stringURL = "myurl.php?deviceName=name'phone"
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!
let url = NSURL(string: encoded)!

print(url)  // "myurl.php%3FdeviceName=name'phone"

Note that ' doesn't have to be encoded, it's a valid URL character.

Update: in the comments you state that it's still not working because your encoded URL is truncated and contains ..., but actually this is likely to be just a printed description truncated by NSURL; the actual URL contained by the NSURL object should be ok. Otherwise it would be nil. You should check on your server side for problems with very long but correct URLs.

这篇关于如何在Swift2中编写“(NSW WindowsCP1250StringEncoding)”等特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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