Objective c NSURL OData URL

Objective c NSURL OData URL,objective-c,nsurl,Objective C,Nsurl,我将我的ODataURL传递给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 an

我将我的ODataURL传递给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,它将变为空

当我给出这样的URL时

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

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

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

在大多数情况下,我都会遇到需要转义URL的情况,方法
stringbyaddingpercentescapesusingencode
就足够了,因为它们是相对较短的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]];

请让我们知道这是否适用于您……

您的URL包含几个必须转义的字符。