7

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?

0

2 Answers 2

36

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Or, you can get an autoreleased object with NSURL * url = [NSURL URLWithString:theTry];
@Tom: I tried "NSURL *url = [NSURL URLWithString:theTry]" and it keeps returning me a null object.
@Dutchie432 & Dave: I also tried "NSURL *url = [[NSURL alloc] initWithString:theTry]", and it too gives me a null object
According to the documentation, URLWithString returning nil means that your string is malformed. More than likely the double quotes (") are messing up your string, and should be escaped.
Escaping (encoding) of URLs is not adding backslashes. See URL encoding: tikirobot.net/wp/2007/01/27/url-encode-in-cocoa
|
1

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]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.