使用 %@ 设置 NSURL 使值等于 %@ 而不是插入 %@
我正在使用 ASIHTTP 并尝试对站点执行 GET 请求:
NSURL *url = [[NSURL URLWithString:@"/verifyuser.aspx?user=%@" relativeToURL:@"http://domain.com"],userName retain];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTemporaryFileDownloadPath:@"myfile2.txt"];
[request setDownloadDestinationPath:@"myfile.txt"];
[request startSynchronous];
但是,当我在 [request startSynchronous] 上放置断点并进入调试器时,请求对象的 url 值等于 userName 变量。我试图将 userName 变量插入到字符串中,然后将其用作 url,因此我的 NSURL 声明中有些问题。
感谢您的帮助!
I'm using ASIHTTP and trying to perform a GET request for a site:
NSURL *url = [[NSURL URLWithString:@"/verifyuser.aspx?user=%@" relativeToURL:@"http://domain.com"],userName retain];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTemporaryFileDownloadPath:@"myfile2.txt"];
[request setDownloadDestinationPath:@"myfile.txt"];
[request startSynchronous];
However, when I put a breakpoint on [request startSynchronous] and go into the debugger, the url value of the request object is equal to the userName variable. I'm trying to insert the userName variable into a string and then use that as the url, so something's not right in my NSURL declaration.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码不正确,您没有正确进行字符串格式化,它应该如下所示:
请注意,您不需要保留 url,因为您只是将其传递给请求。
Your code is incorrect, you are not properly doing string formatting, it should look like this:
Note that you don't need to retain the url as you are just passing it to the request as well.
此 NSURL 方法不需要格式字符串。 Objective-C 编译器对方法的期望做出了相当弱的假设,这可能是您困惑的根源。
使用
[NSString stringWithFormat:@"..."]
代替。目前,您使用的语法解析为
NSURL* url = [(NSURL* object), userName keep];
,正如您可能猜到的那样,这是无效的。事实上,我真的很想知道这是如何编译的。This NSURL method does not expect a format string. The Objective-C compiler makes pretty weak assumptions about what a method expects, which might be the source of your confusion.
Use
[NSString stringWithFormat:@"..."]
instead.Currently the syntax you use resolves to
NSURL* url = [(NSURL* object), userName retain];
, which is, as you might guess, invalid. As a matter of fact, I really wonder how come this compiles.