iphone - 如何在 iOS 的 NSURL 字符串中传递多个参数?

标签 iphone ios objective-c nsurl

我正在开发一个应用程序,我需要在 NSURL 中一次传递多个参数 我的代码是

responseData = [[NSMutableData data] retain];
ArrData = [NSMutableArray array];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@",strfrom,strto,strgo]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
//NSURLRequest *request1 = [NSURLRequest requestWithURL:
//[NSURL URLWithString:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=1",strfrom,strto]];

上面的代码我需要动态传递一个以上的参数。可能吗 ? 如果是,那又如何呢? 谢谢和问候

最佳答案

尝试在添加到 URL 之前创建一个单独的字符串

 NSSString *strURL=[NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@"‌​,strfrom,strto,strgo];

然后将这个 strURL 添加到 URL

NSURL *url = [NSURL URLWithString:strURL];

最后将它添加到请求中,你的代码在你向请求中添加 url 的地方是错误的,URL 不是一个字符串它是一个 URL 所以它应该是 requestWithURL 而不是 URLWithString,应该是这样的

NSURLRequest *request = [NSURLRequest requestWithURL:url];

关于iphone - 如何在 iOS 的 NSURL 字符串中传递多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15089000/

相关文章:

ios - 我可以在低电量时唤醒 iOS 应用程序吗?如何?

iphone - 获取的属性和 NSPredicate

ios - 未声明的类型 adter 'swift build'

ios - 关闭 View Controller 使应用程序崩溃(lldb)

ios - 旋转时无法让背景渐变填满整个屏幕

objective-c - Objective-C 中 dealloc 的首选编码风格是什么?

ios - 为什么 Action 在加载完成后执行多次,为什么它对所有事件进行排队?

iphone - 如何知道 View Controller 是否在堆栈中。如果它在堆栈中,则应将其弹出,否则应将其压入

iphone - 应用程序加载程序警告 : The app references non-public selectors in Payload/<Appname>. app/<App name>: changeState:

ios - tableViewController 不同单元格中的不同图像无法正常工作