我有一个字符串------ NSString abc = @“apple:87,banana:32,grapes:54”;将NSString转换为NSDIctionary
我需要这个输出这样
{
name = "apple";
value = "87";
},
{
name = "banana";
value = "32";
},
{
name = "grapes";
value = "54";
}
我曾尝试:
NSArray* itemList = [abc componentsSeparatedByString:@","];
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for (NSString* item in itemList) {
NSArray* subItemList = [item componentsSeparatedByString:@":"];
if (subItemList.count > 0) {
[dict setObject:[subItemList objectAtIndex:1] forKey:[subItemList objectAtIndex:0]];
}
}
NSLog(@"%@", dict);
输出是 -
{
apple = 87;
banana = 32;
grapes = 54;
}
,但我不希望这个输出
的NSArray * itemList中= [ABC componentsSeparatedByString:@ “”]; NSMutableDictionary * dict = [NSMutableDictionary dictionary]; (itemListList)中的NSString * item { NSArray * subItemList = [item componentsSeparatedByString:@“:”];如果(subItemList.count> 0){[objectItem:[subItemList objectAtIndex:1] forKey:[subItemList objectAtIndex:0]]; } } NSLog(@“%@”,dict); 但我不想要这个输出---- – Eric
请在问题中添加代码,而不是评论。你的代码看起来正是你想要的。但是你的标题描述了你想要作为字典的日志,而且我认为你真正的意思是你想要一个字典数组? – Wain
我认为你需要更清楚你的问题。你不想要什么输出?你想要一个NSDictionary或者你想要一个像你写的第一个字符串(比如'='和';'而不是':'和','的JSON样式? – user1447414