objective c - addingPercentEncoding works different in Swift -


i have obj-c method encodes string:

- (nsstring *) encodevalue:(nsstring*) unescaped{     return [unescaped stringbyaddingpercentencodingwithallowedcharacters:              [nscharacterset urlhostallowedcharacterset]]; } 

input: testswiftapppod://

output: testswiftapppod%3a%2f%2f

i wrote same method in swift got different output: testswiftapppod:%2f%2f

static func encodevalue(unescaped:string!) -> string{    return unescaped.addingpercentencoding(         withallowedcharacters: characterset.urlhostallowed)! } 

for reason colon not converted

how fix issue?

i use xcode 8.3

[edit]

from docs:

// returns new string made receiver replacing characters not in allowedcharacters set percent encoded characters. utf-8 encoding used determine correct percent encoded characters. entire url strings cannot percent-encoded. method intended percent-encode url component or subcomponent string, not entire url string. characters in allowedcharacters outside of 7-bit ascii range ignored. - (nullable nsstring *)stringbyaddingpercentencodingwithallowedcharacters:(nscharacterset *)allowedcharacters ns_available(10_9, 7_0);

edit:

this undocumented intended behavior. see is `addingpercentencoding` broken in xcode 9 beta 2? more details.


this bug.

i went on different cases, seems swift code works correctly. note : allowed in url host, therefore should not encoded , the bug in obj-c version.

nscharacterset *set = [nscharacterset urlhostallowedcharacterset];     nslog(@"colon member: %@", [set characterismember:':'] ? @"true" : @"false"); // prints true 

it's interesting bug because if add ":" character set manually

nsmutablecharacterset *set = [[nscharacterset urlhostallowedcharacterset] mutablecopy]; [set addcharactersinstring:@":"]; 

everything starts work correctly.

report it.

note when encoding url parameters, shouldn't use urlhostallowed. if possible, use nsurlquery build url instead. neither of predefined sets suitable url encoding. can start urlqueryallowed still have remove characters it.

see example this answer correct solution or example implementation in alamofire library.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -