2 starwar2012 starwar2012 于 2013.04.26 11:29 提问

特定字符和NSdictionary

我要转换 " 5?8?519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21 "这个字符串到dictionary。由问号?分开。

预期得到的结果类似:

{
    sometext1 = "5",
    sometext2 = "8",
    sometext3 = "519223cef9cee4df999436c5e8f3e96a",
    sometext4 = "EVAL_TIME",
    sometext5 = "60",
    sometext6 = "2013-03-21"
}

谢谢您的帮助。

1个回答

xiaoyaokk
xiaoyaokk   2013.04.26 12:33
已采纳

先将字符串以"?"分隔成数组,再将数组加到你的字典中

NSString *str = @"5?8?519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21";
NSArray *valueArray = [str componentsSeparatedByString:@"?"];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
for (int i = 0; i <[valueArray count]; i ++) {
    [keyArray addObject:[NSString stringWithFormat:@"sometext%d",i+1]];
}
NSDictionary *dic = [[NSDictionary alloc] initWithObjects:valueArray forKeys:keyArray];
Csdn user default icon
上传中...
上传图片
插入图片