Setting an NSURL with %@ making value equal to %@ instead of inserting %@
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!
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.
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.
精彩评论