Why does [url pathComponents] contain an "/"?
Take for example
NSURL *url = [NSURL URLWithString:@"eg://resources/users/1/answer开发者_开发百科s/2"];
NSLog(@"%@", [url pathComponents]);
>> ( "/", "users", "a", "answers", "b" )
1) Why does the path contain a "/"?
2) If what I want is the "users", is it safe to use the first index?
[[url pathComponents] objectAtIndex:1];
(1) In this case the "/" is the root directory. The other slashes between directories in the path are just separators but the root folder is significant. "users/1/answers/2" may refer to a different location than "/users/1/answers/2", the former is relative to the current directory and the latter always starts at the root folder.
(2) Yes, if the URL is an absolute URL, not for a relative URL. But you can get the absolute URL with the absoluteURL method first and then get the components. Although, what if the URL was just "http://www.example.com/", all you would get is ("/") and that's it, no subdirectory.
精彩评论