我像这样将我的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,它将变为空
当我像这样给出URL时
NSURL *url = [[NSURL alloc] initWithString:@"https://google.com"];则NSURL正在接受该URL...我想知道如何将我的数据URL传递给NSURL。
发布于 2020-03-10 05:57:11
该URL包含几个需要转义的字符。这绝对不是URL的工作方式,因此initWithString方法会失败并返回NULL……
在大多数情况下,我都会遇到需要转义URL的情况,stringByAddingPercentEscapesUsingEncoding方法就足够了,因为它们是相对较短的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]];请让我们知道这是否对你有效...
https://stackoverflow.com/questions/60603818
复制相似问题