iPhone将NSString转换为NSURL错误

时间:2022-02-21 00:14:27

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

我正在尝试将以下NSString api调用转换为NSURL对象:

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?

有人可以告诉我如何将此字符串正确转换为NSURL吗?

2 个解决方案

#1


32  

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

您正在声明NSMutableURLRequest类型的变量并使用NSURL初始化函数(排序)。

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.

注意我已经有一段时间了,因为我做了任何iPhone开发,但我认为这看起来非常准确。

#2


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...

首先,您必须在此行上收到编译错误:NSMutableURLRequest * url = [[NSURL alloc] URLWithString:theTry];但我惊叹你是如何编译的......

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

你做错了是你在类NSURL的实例上调用类方法。

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

URLWithString:是NSURL类的类方法,因此必须将其用作:

NSMutableURLRequest *url = [NSURL URLWithString:url];

and

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

initWithString:是一个实例方法,因此必须将其用作:

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

#1


32  

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

您正在声明NSMutableURLRequest类型的变量并使用NSURL初始化函数(排序)。

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.

注意我已经有一段时间了,因为我做了任何iPhone开发,但我认为这看起来非常准确。

#2


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...

首先,您必须在此行上收到编译错误:NSMutableURLRequest * url = [[NSURL alloc] URLWithString:theTry];但我惊叹你是如何编译的......

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

你做错了是你在类NSURL的实例上调用类方法。

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

URLWithString:是NSURL类的类方法,因此必须将其用作:

NSMutableURLRequest *url = [NSURL URLWithString:url];

and

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

initWithString:是一个实例方法,因此必须将其用作:

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