我要轉換 " 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"
}
謝謝您的幫助。
先將字符串以"?"分隔成數組,再將數組加到你的字典中
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];