所以我有一个像这样的网址的NSString:
NSString stringWithFormat:@"/reading.php?title=blah&description="blah"&image_url=blah... "
将查询字符串附加到此字符串的最佳方法是什么?有没有字典的方法来做到这一点?
答案 0 :(得分:2)
你想做的就是这个。
[NSString stringWithFormat:@"/reading.php?title=blah&description=%@&image_url=blah... ",blah];
答案 1 :(得分:1)
某个类别怎么样?
这不是很好但是第一遍应该会给你一些开始的东西
@interface NSDictionary (ps_additions)
- (NSString *)ps_URLParamsValue;
@end
@implementation NSDictionary (ps_additions)
- (NSString *)ps_URLParamsValue;
{
NSMutableString *params = [NSMutableString string];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
[params appendFormat:@"%@=%@&", key, obj];
}];
return [params copy];
}
@end
最终会出现类似的结果:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"42", @"special_number", @"value", @"another", nil];
NSString *myString = [NSString stringWithFormat:@"/reading.php?%@", [params URLParamsValue]];
NSLog(@"%@", myString);
#=> 2012-03-20 23:54:55.855 Untitled[39469:707] /reading.php?another=value&special_number=42&
答案 2 :(得分:0)
从Paul.s复制粘贴 - 这是正确的答案,imo - 并修复悬挂的&符号的(最可能无关紧要的)问题......
@interface NSDictionary (ps_additions)
- (NSString *)ps_URLParamsValue;
@end
@implementation NSDictionary (ps_additions)
- (NSString *)ps_URLParamsValue;
{
if (!self.count) return @"";
NSMutableString *params = [NSMutableString string];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
[params appendFormat:@"%@=%@&", key, obj];
}];
// return everything except that last ampersand
return [[params copy] substringToIndex:[params length]-1];
}
@end
答案 3 :(得分:0)
您可以使用以下内容:
NSString *parameter1 = @"blah";
NSString *parameter2 = @"anotherblah";
NSString *fullURL = [NSString stringWithFormat:@"/reading.php?title=%@&image_url=%@", parameter1, parameter2];
您可以根据需要添加任意数量的参数。使用“%@”,您将动态添加文本。
祝你好运:)