Why in NSURL trailing slashes initialized by initWithScheme ...?

When using an initializer NSURL

initWithScheme:(NSString *) host:(NSString *) path:(NSString *)


iOS for some reason adds two extra trailing slashes if the path ends with a slash, if only @"/"


enter image description here

Does anyone know why this is the case, and if he has a way other than composing the url manually with something like

[NSURL URLWithString:[NSString stringWithFormat:@"%@://%@%@", scheme, host, path]];


UPDATE: A bug report has been sent to Apple.


source to share

2 answers

File paths start with a "/" character but do not end with a "/" character.

From: Unified Resource Locator :




Directory paths have a trailing "/", but they - initWithScheme:host:path:

don't seem to support them and might just be an Apache convention.



Your path should not end with /

. The following will work:

[[NSURL alloc] initWithScheme:@"http" host:@"example.com" path:@"/hit"])


enter image description here



All Articles