Iphone 如何在iOS中以NSURL字符串传递多个参数?
我正在开发一个应用程序,在该应用程序中,我需要在NSURL中一次传递多个参数 我的代码是Iphone 如何在iOS中以NSURL字符串传递多个参数?,iphone,ios,objective-c,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=%@&am
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];
尝试在添加到URL之前创建一个单独的字符串,例如
NSSString*str=[NSString stringWithFormat:@”http://rate-exchange.appspot.com/currency?from=%@&to=%@&q=%@“,标准格式,标准格式,标准格式]
然后将此str添加到URL,首先将所有参数传递给NSString,然后将最后一个字符串分配给NSURL。我无法完全理解您的问题。您仍然在问题中传递多个参数。现在有什么问题?请尝试使用NSMutableString。这是一个很好的观点,但请注意stringByAddingPercentEscapesUsingEncoding不会对字符串进行完全URL编码。它忽略了诸如/和&。stackoverflow有很多关于如何创建自己的应用程序的例子,但特洛伊敌人的答案对我来说是将unicode单词作为URL的一部分发送的一个组成部分。所以对他来说+1。