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?
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With