使用 %@ 设置 NSURL 使值等于 %@ 而不是插入 %@

发布于 2024-08-17 16:08:52 字数 539 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-08-24 16:08:52

您的代码不正确,您没有正确进行字符串格式化,它应该如下所示:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"/verifyuser.aspx?user=%@", userName] relativeToURL:@"http://domain.com"];

请注意,您不需要保留 url,因为您只是将其传递给请求。

Your code is incorrect, you are not properly doing string formatting, it should look like this:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"/verifyuser.aspx?user=%@", userName] relativeToURL:@"http://domain.com"];

Note that you don't need to retain the url as you are just passing it to the request as well.

妄司 2024-08-24 16:08:52

此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文