Swift 4如何从URLComponent()创建的URL中删除%EF%BB%BF?

时间:2018-10-05 09:53:39

标签: ios swift url nsurlcomponents

我尝试从组件创建URL

var com =  URLComponents()
com.scheme = "http"
com.host = "192.168.1.99"
com.port = 77
com.path = "/api/dosomething/55"

print("SHOW ME URl =  \(com.url!))")

我得到的是这样的东西

http://192.168.1.99:77/%EF%BB%BFapi/dosomething/55

总是得到%EF%BB%BF,在这种情况下,URL无效

我想从URL中删除%EF%BB%BF

我该怎么做?

2 个答案:

答案 0 :(得分:0)

直接回答您的问题

您可以不使用任何方式替换此模式:

let theURL = "http://192.168.1.99:77/%EF%BB%BFapi/dosomething/55"
let partToRemove = "%EF%BB%BF"
let clearURL = theURL.replacingOccurrences(of: partToRemove, with: "")
print(clearURL)

答案 1 :(得分:0)

这是UTF-8 BOM的URL编码,可能是由某些文本编辑器对URL的处理引起的。修剪路径字符串中的空格:

var com =  URLComponents()
com.scheme = "http"
com.host = "192.168.1.99"
com.port = 77
let path = //The path
let trimmedPath = path.trimmingCharacters(in: .whitespaces)
com.path = trimmedPath

print("SHOW ME URl =  \(com.url!)")

在utf-8中,'ZERO WIDTH NO-BREAK SPACE'被编码为0xEF 0xBB 0xBF