Why is the return value of String.addingPercentEncoding() optional?

后端 未结 2 943
醉话见心
醉话见心 2020-12-03 07:11

The signature of the String method for percent-escaping is:

func addingPercentEncoding(withAllowedCharacters: CharacterSet)
    -> String?
         


        
2条回答
  •  囚心锁ツ
    2020-12-03 07:29

    Based on Paul Cantrell answer, small demonstration that it's also possible for the same method to also return null in Objective-C, despite String and NSString being different beasts when it comes to encodings:

    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);
    

    For fun, https://r12a.github.io/app-conversion/ will percent escape the same as:

    Error%20in%20convertUTF162Char%3A%20low%20surrogate%20expected%2C%20b%3D0%21%00

提交回复
热议问题