使用块将 NSDictionary 转换为字符串?

发布于 2024-09-26 19:12:39 字数 627 浏览 7 评论 0原文

我确信有一种方法可以使用块来做到这一点,但我无法弄清楚。我想将 NSDictionary 转换为 url 样式的参数字符串。 如果我有一个如下所示的 NSDictionary:

dict = [NSDictionary dictionaryWithObjectsAndKeys:@"blue", @"color", @"large", @"size", nil]];

那么我如何将其转换为如下所示的字符串:

"color=blue&size=large"

编辑

感谢以下线索。这应该可以做到:

NSMutableString *parameterString;
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [parameterString appendFormat:@"%@=%@&", key, obj];
}];
parameterString = [parameterString substringToIndex:[string length] - 1];

I'm sure there is a way to do this using blocks, but I cant figure it out. I want to to turn an NSDictionary into url-style string of parameters.
If I have an NSDictionary which looks like this:

dict = [NSDictionary dictionaryWithObjectsAndKeys:@"blue", @"color", @"large", @"size", nil]];

Then how would I turn that into a string that looks like this:

"color=blue&size=large"

EDIT

Thanks for the clues below. This should do it:

NSMutableString *parameterString;
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [parameterString appendFormat:@"%@=%@&", key, obj];
}];
parameterString = [parameterString substringToIndex:[string length] - 1];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞清仓 2024-10-03 19:12:39

完全相同的解决方案,但没有子字符串:

NSMutableArray* parametersArray = [[[NSMutableArray alloc] init] autorelease];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [parametersArray addObject:[NSString stringWithFormat:@"%@=%@", key, obj]];
}];
NSString* parameterString = [parametersArray componentsJoinedByString:@"&"];

Quite same solution but without the substring:

NSMutableArray* parametersArray = [[[NSMutableArray alloc] init] autorelease];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [parametersArray addObject:[NSString stringWithFormat:@"%@=%@", key, obj]];
}];
NSString* parameterString = [parametersArray componentsJoinedByString:@"&"];
黎夕旧梦 2024-10-03 19:12:39

创建一个可变字符串,然后迭代字典获取每个键。查找该键的值,并将 key=value& 添加到字符串中。完成该循环后,删除最后一个 &。

我认为这将通过 URL 提供,您还需要一些对字符串进行编码的方法,以防它们包含类似 & 的项目。或+等

Create a mutable string, then iterate the dictionary getting each key. Look up the value for that key, and add the key=value& to the string. When you finish that loop, remove the last &.

I presume this is going to be fed through a URL, you will also want to have some method that encodes your strings, in case they contain items like & or +, etc.

梦晓ヶ微光ヅ倾城 2024-10-03 19:12:39
NSMutableString* yourString = @"";

for (id key in dict) {
     [yourString appendFormat:@"%@=%@&", key, ((NSString*)[dict objectForKey:key])];
}

NSRange r;
r.location = 0;
r.size = [yourString length]-1;
[yourString deleteCharactersInRange:r];
NSMutableString* yourString = @"";

for (id key in dict) {
     [yourString appendFormat:@"%@=%@&", key, ((NSString*)[dict objectForKey:key])];
}

NSRange r;
r.location = 0;
r.size = [yourString length]-1;
[yourString deleteCharactersInRange:r];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文