为什么String.addingPercentEncoding()的返回值是可选的?

时间:2015-11-06 03:25:20

标签: swift cocoa unicode utf-8 url-encoding

用于转义百分比的String方法的签名是:

func addingPercentEncoding(withAllowedCharacters: CharacterSet)
    -> String?

(在Swift 2中这是stringByAddingPercentEncodingWithAllowedCharacters。)

为什么这个方法会返回一个可选的?

文档说该方法返回nil“如果转换不可能”,但不清楚在什么情况下转义转换可能会失败:

  • 使用UTF-8转义字符,这是一种完整的Unicode编码。任何有效的Unicode字符都可以使用UTF-8进行编码,因此可以进行转义。

  • 我想也许这个方法对允许的字符集和用于转义的字符之间的不良交互应用了某种形式的检查,但事实并非如此:无论是否允许,该方法都会成功chars包含“%”,如果允许的char集为空,也会成功。

按照目前的情况,非可选的返回值似乎强制进行无意义的错误检查。

2 个答案:

答案 0 :(得分:33)

我向Apple提交了一份有关此问题的错误报告,并收到了回复 - 非常有帮助的回复,不会少!

结果(令我惊讶的是)可以成功创建包含无配对UTF-16代理字符形式的无效Unicode的Swift字符串。这样的字符串可能导致UTF-8编码失败。以下是一些说明此行为的代码:

// Succeeds (wat?!):
let str = String(
    bytes: [0xD8, 0x00] as [UInt8],
    encoding: String.Encoding.utf16BigEndian)!

// Returns nil:
str.addingPercentEncoding(withAllowedCharacters:
    CharacterSet.alphanumerics)

答案 1 :(得分:1)

根据Paul Cantrell的回答,尽管String和NSString在编码方面不同,但同样的方法也可能在Objective-C中返回null的小型演示:

uint8_t bytes[2] = { 0xD8, 0x00 };
NSString *string = [[NSString alloc] initWithBytes:bytes length:2 encoding:NSUTF16BigEndianStringEncoding];
// \ud800
NSLog(@"%@", string);

NSString *escapedString = [string stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet];
// (null)
NSLog(@"%@", escapedString);

为了好玩,https://r12a.github.io/app-conversion/将百分之百逃避:

  

错误%20英寸%20convertUTF162Char%3A%20low%20surrogate%20expected%2C%20B%3D0%21%00