我在这里有这个动作:
- (IBAction)searchButton:(id)sender {
NSString *textString = self.symbolSearchField.text;
NSURL *sourceURL = [[NSURL alloc] initWithString: [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20yahoo.finance.quotes%%20where%%20symbol%%20in%%20%%28%%22%@%%22%%29&env=store://datatables.org/alltableswithkeys", textString]];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:sourceURL];
parser.delegate = self;
[parser parse];
我希望它从搜索字段中获取文本,使用该文本修改URL,并从URL解析XML。但是,当我转义* s和%s时,似乎URL变为“已损坏”,并且它不会解析。如果我保留URL而不转义...
NSURL *sourceURL = [[NSURL alloc] initWithString: [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22%@%22%29&env=store://datatables.org/alltableswithkeys", textString]];
...然后我收到“无效的转化说明符'*'”和“比数据参数更多'%'次转化”警告。
所以我的问题是,如何在不破坏URL的情况下转义符号?
修改
为清楚起见,这是我做出一些调整后的代码。
NSString *textString = self.symbolSearchField.text;
NSString *query = [NSString stringWithFormat:@"select * from yahoo.finance.quotes where symbol in (\"%@\")", textString];
NSString *escapedStoreURL = [@"store://datatables.org/alltableswithkeys" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *escapedQuery = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *URLString = [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=%@&env=%@", escapedQuery, escapedStoreURL];
NSURL *sourceURL = [NSURL URLWithString:URLString];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:sourceURL];
parser.delegate = self;
[parser parse];
答案 0 :(得分:0)
试试这个:
NSString *firstPart = @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22";
NSString *lastPart = @"%22%29&env=store://datatables.org/alltableswithkeys";
NSURL *sourceURL = [[NSURL alloc] initWithString: [NSString stringWithFormat:@"%@%@%@", firstPart, textString, lastPart]];
您可能还想考虑以编程方式转义该查询,而不是事先在那里进行所有转义。商店网址也需要进行网址编码。
NSString *query = [NSString stringWithFormat:@"select * from yahoo.finance.quotes where symbol in (\"%@\")", textString];
NSString *escapedStoreUrl = [@"store://datatables.org/alltableswithkeys" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *escapedQuery = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *urlString = [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=%@&env=%@", escapedQuery, escapedStoreUrl];
所以我回来了,再次看到这个问题,事实证明在原来这是caused by StackOverflow。yahoo.finance.quotes
中的u和o与alltableswithkeys
的两个Ls之间存在一些不可见的字符(具体来说,U+200c)中的两个。删除这些应该可以解决您的问题
答案 1 :(得分:0)
为什么不直接使用NSMutableString,并逐步附加字符串?
NSString *textString = @"AAPL";
NSMutableString *query = [[NSMutableString alloc] init];
NSString *startString = @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22";
NSString *endString = @"%22%29&env=store://datatables.org/alltableswithkeys";
[query appendString:startString];
[query appendFormat:@"%@",textString];
[query appendString:endString];
NSURL *sourceURL = [[NSURL alloc] initWithString:query];
现在,变量sourceURL
是您的网址,其符号为“已转义”。