IPhone converts NSString to NSURL error

I am trying to convert the following ApiNSString call to an NSURL object:

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

Here are the objects that I have installed. I escaped the backslash quotes:

NSString *theTry=@"http://beta.com/api/token=\"69439028\""; NSLog(@"theTry=%@",theTry); NSMutableURLRequest *url = [[NSURL alloc] URLWithString:theTry]; NSLog(@"url=%@",url); 

Every time 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 tell me how I can correctly convert this string to NSURL?

+7
objective-c iphone
source share
2 answers

You declare a variable of type NSMutableURLRequest and use the NSURL initialization function (sort).

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

to try

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

Please note that some time has passed since I made the iPhone, but I think it looks pretty accurate.

+31
source share

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

What you are doing wrong, you call the class method on an instance of the NSURL class ...

-> URLWithString: the class method of the NSURL class, so you should use it like:

 NSMutableURLRequest * url = [NSURL URLWithString:url]; 

-> and initWithString: instance method, so you should use it like:

 NSMutableURLRequest * url = [[NSURL alloc] initWithString:url]; 
0
source share

All Articles