iPhone 将 NSString 转换为 NSURL 错误

标签 iphone objective-c

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

http://beta.com/api/token= “69439028”

这是我设置的对象。我用反斜杠转义了引号:

NSString *theTry=@"http://beta.com/api/token=\"69439028\"";
NSLog(@"theTry=%@",theTry);


NSMutableURLRequest *url = [[NSURL alloc] URLWithString:theTry];
NSLog(@"url=%@",url);

每次我运行这个,我都会收到这个错误:

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'

谁能告诉我如何正确地将此字符串转换为 NSURL?

最佳答案

您正在声明一个 NSMutableURLRequest 类型的变量并使用 NSURL 初始化函数(某种程度上)。

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

尝试

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

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

关于iPhone 将 NSString 转换为 NSURL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3356852/

相关文章:

iphone - initWithNibName 不工作

objective-c - Dateformatter 更改日期的年份

ios - 用嵌套 block 保留 self ?

ios - 如何在 Swift 3 中加载/删除带有动画的自定义 View

iPhone 在连接到 Mac 时显示问号(代替其图标)

javascript - 如何从网页访问 iPhone 的摄像头?

ios - CALayer 不会针对不同的方向使用渐变颜色自动调整大小

iphone - 使用社交框架将图像添加到 Facebook

iphone - iPhone sdk中的sqlite外键

iphone - 使用用户 token 、rest/rails 和钥匙串(keychain)帮助进行 iOS 身份验证