将NSDictionary解析为包含自定义分隔符的字符串 [英] Parse NSDictionary to a string with custom separators

查看:232
本文介绍了将NSDictionary解析为包含自定义分隔符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些值的NSMutableDictionary,我需要将键和值连接成一个字符串,所以

I have an NSMutableDictionary with some values in it, and I need to join the keys and values into a string, so

> name = Fred
> password = cakeismyfavoritefood
> email = myemailaddress@is.short

变为 name = Fred& password = cakeismyfavoritefood& ; email=myemailaddress@is.short

我该怎么做?有没有办法将 NSDictionaries 加入字符串?

How can I do this? Is there a way to join NSDictionaries into strings?

推荐答案

你可以轻松地枚举字典键和对象:

You can easily do that enumerating dictionary keys and objects:

NSMutableString *resultString = [NSMutableString string];
for (NSString* key in [yourDictionary allKeys]){
    if ([resultString length]>0)
       [resultString appendString:@"&"];
    [resultString appendFormat:@"%@=%@", key, [yourDict objectForKey:key]];
}

这篇关于将NSDictionary解析为包含自定义分隔符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆