2

I need to convert this "5?8?519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21" string into dictionary. Separated by "?"

Dictionary would be some thing like

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

Thank you .

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
Muhammad Saad Ansari
  • 1,748
  • 3
  • 13
  • 20
  • 1
    If any of below answer helps you please accept one , so the others can easily found the solution – Raj Apr 23 '13 at 10:38

4 Answers4

6

Break the string to smaller strings and loop for them. This is the way

NSArray *objects = [inputString componentsSeparatedByString:@"?"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
int i = 1;
for (NSString *str in objects)
{
    [dict setObject:str forKey:[NSString stringWithFormat:@"sometext%d", i++]];
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Avi Tsadok
  • 1,843
  • 13
  • 19
  • your logic goes fine, but you had 4-5 syntax errors!! I corrected those. – Anoop Vaidya Apr 21 '13 at 09:03
  • Thank you but keys has to be different . Any thing like xyz , abc , etc sometext1,2,3 is just for giving you an idea . – Muhammad Saad Ansari Apr 21 '13 at 09:03
  • @MuhammadSaadAnsari: from where are you getting those keys? – Anoop Vaidya Apr 21 '13 at 09:04
  • { cid = "5", avid = "8", sid = "519223cef9cee4df999436c5e8f3e96a", TLicense = "EVAL_TIME", LLicense = "60", date = "2013-03-21" } . This is how i want this dictionary. These keys are generating from my self only for future use but that string i am geting from server. – Muhammad Saad Ansari Apr 21 '13 at 09:10
  • I got the answer by myself . Thank you all of you. :) This is what i am doing NSString *testString = @"5?8?519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21"; NSArray *list = [testString componentsSeparatedByString:@"?"]; NSLog(@"Count %d",[list count]); NSLog(@"Dictionary %@", [NSDictionary dictionaryWithObjects:list forKeys:[NSArray arrayWithObjects:@"cid",@"avid",@"sid",@"tlicense",@"llicense",@"date", nil]]); – Muhammad Saad Ansari Apr 21 '13 at 09:17
  • @MuhammadSaadAnsari That would work fine as long as the inputString is proper. – Anupdas Apr 21 '13 at 09:18
  • I didn't write in xCode, but in the browser, and from my iPhone. That's why I had syntax errors...sorry! – Avi Tsadok Apr 21 '13 at 10:09
  • Hi, how can i separate two values from NSDictionary am getting response like this { "Result" : "Select~, CUSTDAM~CUSTOMER DAMAGE, PM~PREVENTATIVE MAINTENANCE, WEAR~WEAR AND TEAR" } - How do i separate Values before the ~ symbol ex - Select,CUSTDAM,PM,WEAR – Apple Apr 24 '15 at 05:54
4

Try

NSString *string = @"5?8?3519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21";

NSArray *stringComponents = [string componentsSeparatedByString:@"?"];
//This is very risky, your code is at the mercy of the input string
NSArray *keys = @[@"cid",@"avid",@"sid",@"TLicense",@"LLicense",@"date"];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (int idx = 0; idx<[stringComponents count]; idx++) {
    NSString *value = stringComponents[idx];
    NSString *key = keys[idx];
    [dictionary setObject:value forKey:key];
}

EDIT: More optimized

NSString *string = @"5?8?3519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21";

NSArray *stringComponents = [string componentsSeparatedByString:@"?"];
NSArray *keys = @[@"cid",@"avid",@"sid",@"TLicense",@"LLicense",@"date"];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjects:stringComponents forKeys:keys];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
0

first separate the string into several arrays by '?'.

then add the string in you dictionary.

sth like this:

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];
Winnie
  • 769
  • 5
  • 23
0

For the future: If you were to store your data in JSON format (closer to what you have anyway), it'll be much easier to deal with and transfer between systems. You can easily read it...using NSJSONSerialization

Komposr
  • 1,946
  • 1
  • 12
  • 4