首页
学习
活动
专区
圈层
工具
发布

NSURL URL
EN

Stack Overflow用户
提问于 2020-03-09 23:34:08
回答 1查看 35关注 0票数 0

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

代码语言:javascript
复制
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时

代码语言:javascript
复制
NSURL *url = [[NSURL alloc] initWithString:@"https://google.com"];

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-10 05:57:11

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

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

对于这个特定的场景,下面的方法可能会在保持简单的同时产生最好的结果。

代码语言:javascript
复制
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]];

请让我们知道这是否对你有效...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60603818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档