NSURL 与相对于 URL 的字符串返回意外结果

NSURL with string relative to URL returning unexpected result

这是我正在拨打的电话:

NSURL *url = [NSURL URLWithString:@"rooms" relativeToURL:[NSURL URLWithString:@"localhost:9000"]];

而url.absoluteURL的结果是:

localhost:///rooms

这有意义吗?我期待结果是 localhost:9000/rooms

谢谢!

您缺少 schemeNSURL 似乎将 localhost: 部分解释为方案,这会导致意外行为。如果是 http 你应该试试这个:

NSURL *url = [NSURL URLWithString:@"rooms" relativeToURL:[NSURL URLWithString:@"http://localhost:9000/"]];