I have obj-C method that encodes String:
- (NSString *) encodeValue:(NSString*) unescaped{
return [unescaped stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet URLHostAllowedCharacterSet]];
}
input: testswiftapppod://
output: testswiftapppod%3A%2F%2F
I wrote the same method in Swift but got different output: testswiftapppod:%2F%2F
static func encodeValue(unescaped:String!) -> String{
return unescaped.addingPercentEncoding(
withAllowedCharacters: CharacterSet.urlHostAllowed)!
}
For some reason colon not converted
How to fix this issue?
I use Xcode 8.3
[EDIT]
From Docs:
// Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters. UTF-8 encoding is used to determine the correct percent encoded characters. Entire URL strings cannot be percent-encoded. This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored. - (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters NS_AVAILABLE(10_9, 7_0);