Xcode 9中的"addingPercentEncoding"有问题吗? [英] is `addingPercentEncoding` broken in Xcode 9?

查看:249
本文介绍了Xcode 9中的"addingPercentEncoding"有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有Xcode 9 beta 2的Swift 3.x中,使用 addingPercentEncoding 给出了意外的结果. CharacterSet.urlPathAllowed始终包含:",因此根据addingPercentEncoding的定义,它永远不能转义.但是,使用此代码:

// always true
print(CharacterSet.urlPathAllowed.contains(":"))
let myString = "info:hello world"
let escapedString = myString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!
print(escapedString)

我得到那些结果:

出现不良行为的情况

  • Xcode 9 beta 2,iOS 9.3
  • Xcode 9 beta 2,iOS 11.0

    true
    info%3Ahello%20world

出现预期行为的情况

  • Xcode 9 beta 2,iOS 10.3.1
  • Xcode 8.3.3,任何iOS

    true
    信息:hello%20world

是否有任何变通办法来获得可以正常使用给定allowedCharactersaddingPercentEncoding的有效实现?

解决方案

显然,当用作参考的CharacterSet是基础NSCharacterSet类时,addingPercentEncoding会完成一些未公开的魔术.

因此,要变通解决此魔术,您需要使CharacterSet成为纯Swift对象.为此,我将创建一个副本(感谢Martin R!),以使邪恶的魔咒消失:

let myString = "info:hello world"
let csCopy = CharacterSet(bitmapRepresentation: CharacterSet.urlPathAllowed.bitmapRepresentation)
let escapedString = myString.addingPercentEncoding(withAllowedCharacters: csCopy)!
//always "info:hello%20world"
print(escapedString)

作为扩展:

extension String {
    func safeAddingPercentEncoding(withAllowedCharacters allowedCharacters: CharacterSet) -> String? {
        // using a copy to workaround magic: https://stackoverflow.com/q/44754996/1033581
        let allowedCharacters = CharacterSet(bitmapRepresentation: allowedCharacters.bitmapRepresentation)
        return addingPercentEncoding(withAllowedCharacters: allowedCharacters)
    }
}

in Swift 3.x with Xcode 9 beta 2, using addingPercentEncoding gives unexpected results. CharacterSet.urlPathAllowed always contains ":", so by definition of addingPercentEncoding, it should never escape it. Yet, using this code:

// always true
print(CharacterSet.urlPathAllowed.contains(":"))
let myString = "info:hello world"
let escapedString = myString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!
print(escapedString)

I get those results:

cases where I get an undesirable behavior

  • Xcode 9 beta 2, iOS 9.3
  • Xcode 9 beta 2, iOS 11.0

    true
    info%3Ahello%20world

cases where I get the expected behavior

  • Xcode 9 beta 2, iOS 10.3.1
  • Xcode 8.3.3, any iOS

    true
    info:hello%20world

Is there any workaround to get a working implementation of addingPercentEncoding that will correctly respect the given allowedCharacters?

解决方案

Apparently there is some undocumented magic done by addingPercentEncoding when the CharacterSet used as reference is an underlying NSCharacterSet class.

So to workaround this magic, you need to make your CharacterSet a pure Swift object. To do so, I'll create a copy (thanks Martin R!), so that the evil magic is gone:

let myString = "info:hello world"
let csCopy = CharacterSet(bitmapRepresentation: CharacterSet.urlPathAllowed.bitmapRepresentation)
let escapedString = myString.addingPercentEncoding(withAllowedCharacters: csCopy)!
//always "info:hello%20world"
print(escapedString)

As an extension:

extension String {
    func safeAddingPercentEncoding(withAllowedCharacters allowedCharacters: CharacterSet) -> String? {
        // using a copy to workaround magic: https://stackoverflow.com/q/44754996/1033581
        let allowedCharacters = CharacterSet(bitmapRepresentation: allowedCharacters.bitmapRepresentation)
        return addingPercentEncoding(withAllowedCharacters: allowedCharacters)
    }
}

这篇关于Xcode 9中的"addingPercentEncoding"有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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