views:

442

answers:

2

Hi, 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!

+4  A: 

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.

Greg Martin
See the documentation for NSString: http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:
Eld
Well, when I do that, my program gets SIGABRT on that line with this in the console:-[NSCFString absoluteURL]: unrecognized selector sent to instance 0x2630c2010-01-15 18:32:16.013 HTTPExampleProject[18253:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString absoluteURL]: unrecognized selector sent to instance 0x2630c'
quantumpotato
Can you post your updated code?
Greg Martin
My mistake on something else, sorry. Thanks :)
quantumpotato
A: 

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.

zneak