Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone convert NSString to NSURL error

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?

like image 371
dpigera Avatar asked Jul 28 '10 19:07

dpigera


2 Answers

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.

like image 174
Dutchie432 Avatar answered Oct 13 '22 20:10

Dutchie432


First of all, you must get a compilation error on this line: NSMutableURLRequest *url = [[NSURL alloc] URLWithString:theTry]; But I marvel how you did compile it...

What you are doing wrong is you are calling a class method on instance of class NSURL.

URLWithString: is a class method of NSURL class, so you must use it as:

NSMutableURLRequest *url = [NSURL URLWithString:url];

and

initWithString: is an instance method, so you must use it as:

NSMutableURLRequest *url = [[NSURL alloc] initWithString:url];
like image 44
Nitesh Borad Avatar answered Oct 13 '22 22:10

Nitesh Borad