带有参数的URL字符串的问题

时间:2013-06-04 18:51:07

标签: ios string url stringwithformat

我需要将字符串参数startDate和endDate插入到URL字符串中,我正在尝试使用stringWithFormat,但xcode会抱怨URL字符串中的许多其他字符,它可能会将其解释为无效的说明符。

NSString *urlString = [NSString stringWithFormat:@"https://api.xmltime.com/astronomy?accesskey=l1fMY6Om37&expires=2013-06-05T18%3A00%3A00%2B00%3A00&signature=12P2tr1MyD4NnLjqhWzc%2BpsBKR0%3D&object=sun&placeid=netherlands%2Famsterdam&startdt=2013-06-04&enddt=2013-07-04&geo=0&isotime=0&lang=en&utctime=0&types=all", startDate, endDate];

我将变量与%@说明符放在2013-06-04和2013-07-04字符串的位置。我猜xcode不喜欢字符串中的其他百分号。在此字符串中设置参数的正确方法是什么?谢谢。

1 个答案:

答案 0 :(得分:0)

使用%%%放入stringWithFormat。

所以你的代码变成了:

NSString *urlString = [NSString stringWithFormat:@"https://api.xmltime.com/astronomy?accesskey=l1fMY6Om37&expires=2013-06-05T18%%3A00%%3A00%%2B00%%3A00&signature=12P2tr1MyD4NnLjqhWzc%%2BpsBKR0%%3D&object=sun&placeid=netherlands%%2Famsterdam&startdt=2013-06-04&enddt=2013-07-04&geo=0&isotime=0&lang=en&utctime=0&types=all", startDate, endDate];

相关问题