带有变量的NSURL中的空格不会加载 [英] Spaces in a NSURL with variables doesn't load

查看:106
本文介绍了带有变量的NSURL中的空格不会加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有一个目标-c困境:P
我对objective-c很新,我尝试寻找答案,但无济于事。

Hello everyone I have an objective-c dilema :P I am quite new to objective-c, and I have tried searching for an answer, but to no avail.

所以,这是我的情况。
我现在将代码放在这里,否则它不会有多大意义。
我正在放下我需要的东西,这段代码现在不能正常工作,我会在以后找到原因

So, here is my situation. I will put the code here now, or else it won't make as much sense. I am putting down what I need, this code does not work right now, and I will get to why later

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
NSString *monthString = [formatter stringFromDate:date];
NSLog(@"MONTH STRING %@", monthString);
NSString *baseURLStr = @"http://www.mysite.ca/example";
NSURL *url = [NSURL URLWithString:[baseURLStr stringByAppendingFormat:@"announcements%20%@%20%d%20carson.ashx", monthString]];

[webView loadRequest:[NSURLRequest requestWithURL:url]];
[NSCalendar release];

所以,这就是问题所在......编码(HTML):stringByAppendingFormat:@公告%@%20%d%20example.ashx,monthString,day]];

So, this is where the problem lies... Encoded (HTML): stringByAppendingFormat:@"announcements%@%20%d%20example.ashx", monthString, day]];

解码它看起来像这样

stringByAppendingFormat:@公告%@%d example.ashx]];

stringByAppendingFormat:@"announcements %@ %d example.ashx"]];

这应该更容易理解..
当我跑步时%20,它说找不到请求的文件
当我用空格()运行它只是白色,没有任何加载。

That should be easier to understand.. When I run with the %20's, it says "The requested document was not found" When I run with spaces (" ") it is just white, and nothing loads.

我知道URL是正确的,如果我把变量拿出来,这是完全相同的问题,但是当我将%20移回baseURLStr时,它可以正常工作并加载,所以它与HTML代码和stringbyAppendingFormat字符串。

I know the URL is correct, and if I take the variables out, it is the exact same problem, but when I move the %20's back to the baseURLStr, it works and loads just fine, so it is something to do with the HTML Codes and the "stringbyAppendingFormat" string.

感谢任何帮助!

感谢您的时间

-Kyle

推荐答案

每当NSURL在init之后返回nil(0x0)这是almos t总是与不正确的url字符串有关。并且非常挑剔地获取格式正确的字符串。

whenever an NSURL returns nil (0x0) after init it is almost always related to in improper url string. And its very picky about getting a properly formatted string.

我的建议是简单地构建你的字符串,没有任何转义或url编码,然后只需调用

my recommendation is to simply build your string, without any escapes or url encoding, then simply call

myUrlString = [myUrlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

这里是它的标题声明

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc NS_AVAILABLE(10_3, 2_0);

这样,我总是知道我按照NSURL类想要的方式格式化它。

this way, I always know I get it formatted the way that the NSURL class wants it.

这是一个例子

NSString *sUrl = @"http://www.myside.ca/example/announcements carson.ashx"; //notice the embedded space
sUrl = [sUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:sUrl];

这篇关于带有变量的NSURL中的空格不会加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆