本文介绍了将NSString转换为NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从通过其描述
方法创建的字符串中返回 NSDictionary
>
给定此代码:
NSDictionary * dictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@value 1,@value 2,nil] forKeys:[NSArray arrayWithObjects:@key 1,@key 2,nil]
NSString * dictionaryString = [dictionary description];
NSLog(dictionaryString);
这使得此输出:
{
key 1=value 1;
key 2=value 2;
}
有没有一个简单的方法从字符串回到NSDictionary? / p>
解决方案
有一个愚蠢的问题:你为什么要这样做?如果要去往和来自文本,请使用NSPropertyListSerialization类。
Is there a way to get an NSDictionary
back from the string created via its description
method?
Given this code:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"value 1", @"value 2", nil] forKeys:[NSArray arrayWithObjects:@"key 1", @"key 2", nil]];
NSString *dictionaryString = [dictionary description];
NSLog(dictionaryString);
Which makes this output:
{
"key 1" = "value 1";
"key 2" = "value 2";
}
Is there an easy way to go back from the string to an NSDictionary?
解决方案
At the risk of asking a stupid question: why would you want to do this? If you want to go to and from text, use the NSPropertyListSerialization class.
这篇关于将NSString转换为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!