我正在尝试学习ios,我的第一个项目是创建一个应用程序,在用户输入的邮政编码区域显示天气预报。
我的网址是:
NSString *zipUrl = @"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2289448%22&format=json";
邮政编码接近3D%22后的结尾:
3D%22 * 89448 *%22
我只需要弄清楚如何将用户在文本框中输入的内容插入此位置。我已经尝试[NSString StringWithFormat]
,但有很多%的迹象表明它不起作用。
如果您需要更多信息,请与我们联系。
答案 0 :(得分:1)
您需要首先解码zipUrl,因为它编码了字符串。
NSString *zipUrl = @"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2289448%22&format=json";
NSString *decodedString= [[NSString stringWithFormat:@"%@",zipUrl] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"decodedString : %@",decodedString);
NSString *myStringURL = [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=select item from weather.forecast where location=%@&format=json",yourtextfield.text];
希望它对你有所帮助。
答案 1 :(得分:0)
您可以通过%@
符号输入/添加/设置用户输入的值,例如
NSString *urlString = [NSString stringWithFormat:@"http://query.yahooapis.com/userInput=%@",textFiled.text];
并使用stringByAddingPercentEscapesUsingEncoding将您的网址转换为legal URL
NSString *urlString = [NSString stringWithFormat:@"URL_STRING"];
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];