【问题标题】:NSURL OData URLNSURL OData URL
【发布时间】:2020-06-21 12:51:20
【问题描述】:

我像这样将我的 OData URL 传递给 NSURL

NSURL *url = [[NSURL alloc] initWithString:@"https://localhost/odata/Venues?$expand=Fixtures($filter=day(DateTime) eq 9 and month(DateTime) eq 3 and year(DateTime) eq 2020)&$filter=Fixtures/any(f: day(f/DateTime) eq 9 and month(f/DateTime) eq 3 and year(f/DateTime) eq 2020)"];

NSURL 不接受这个 URL,它正在变为 NULL

当我给出这样的 URL 时

NSURL *url = [[NSURL alloc] initWithString:@"https://google.com"];

那么 NSURL 正在接受 URL...我想知道如何将我的数据 URL 传递给 NSURL。

【问题讨论】:

  • 您的网址包含多个必须转义的字符。

标签: objective-c nsurl


【解决方案1】:

该 URL 包含几个需要转义的字符。这绝对不是 URL 的工作方式,因此方法 initWithString 失败并返回 NULL...

在大多数情况下,我都遇到了需要转义 URL 的情况,方法 stringByAddingPercentEscapesUsingEncoding 就足够了,因为它们是相对较短的 URL。尽管如此,您的 URL 是一个包含很多字符的 URL,这种方法并不特别喜欢。 (这包括斜线 / 和 & 符号 &)。

对于这种特殊情况,以下方法可能会在保持简单的同时产生最佳结果。

NSString* unencodedURLString = @"https://localhost/odata/Venues?$expand=Fixtures($filter=day(DateTime) eq 9 and month(DateTime) eq 3 and year(DateTime) eq 2020)&$filter=Fixtures/any(f: day(f/DateTime) eq 9 and month(f/DateTime) eq 3 and year(f/DateTime) eq 2020)";

NSString* urlString = [unencodedURLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

如果这对您有用,请告诉我们...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    相关资源
    最近更新 更多