繁体   English   中英

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

[英]How can pass multiple parameters in NSURL string in iOS?

我在该应用程序中开发一个应用程序我需要在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];

许多这些答案缺少的一件事是使用[NSString stringByAddingPercentEscapesUsingEncoding:]来避免在URL中使用无效字符:

NSString *baseURL = [NSString stringWithFormat:@"http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@",strfrom,strto,strgo];
NSURL *url = [NSURL URLWithString:[baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM