views:

261

answers:

1

I'm trying to convert the following NSString api call to a NSURL object:

http://beta.com/api/token="69439028"

Here are the objects I have setup. I have escaped the quotation marks with backslashes:

NSString *theTry=@"http://beta.com/api/token=\"69439028\"";
NSLog(@"theTry=%@",theTry);


NSMutableURLRequest *url = [[NSURL alloc] URLWithString:theTry];
NSLog(@"url=%@",url);

Everytime I run this, I keep getting this error:

2010-07-28 12:46:09.668 RF[10980:207] -[NSURL URLWithString:]: unrecognized selector sent to instance 0x5c53fc0
2010-07-28 12:46:09.737 RF[10980:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL URLWithString:]: unrecognized selector sent to instance 0x5c53fc0'

Can someone please tell me how i can convert this String into an NSURL correctly?

+3  A: 

You are declaring a variable of type NSMutableURLRequest and using an NSURL initialization function (sort of).

NSMutableURLRequest *url = [[NSURL alloc] URLWithString:theTry];

try

NSURL *url = [[NSURL alloc] initWithString:theTry];

Note it's been a while since I did any iPhone dev, but I think this looks pretty accurate.

Dutchie432
Or, you can get an autoreleased object with NSURL * url = [NSURL URLWithString:theTry];
Tom Irving
@Tom: I tried "NSURL *url = [NSURL URLWithString:theTry]" and it keeps returning me a null object.
dpigera
dpigera
According to the [documentation](http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString:), URLWithString returning nil means that your string is malformed. More than likely the double quotes (") are messing up your string, and should be escaped.
David Liu
hmm. i did escape them with quotation marks. however the api call i'm implementing explicitly states that I need to surround the last token number with quotation marks :-/
dpigera
Escaping (encoding) of URLs is not adding backslashes. See URL encoding: http://www.tikirobot.net/wp/2007/01/27/url-encode-in-cocoa/
Alex Reynolds
Once you have an encoded string, you can make an NSURL from it.
Alex Reynolds