-1

Hello Guys I have one string For example

"Name=Mihir&,Age=34&,Allergy=&,Symptoms=Fever,Sore throat&,Tested=No&,State=Rhode Island"

So I need to convert this string into 2 arrays key and values like one array before = and another array after equal to.
I have done some code but I don't know what to do next!
Please see my code below:

strCustomFields = _incident.CustomFields;
NSLog(@"strCustomFields = %@",strCustomFields);
NSArray *items = [strCustomFields componentsSeparatedByString:@"&,"];  

Here I have separated into array So now I have 6 arrays but now how to make my array to key and value like below?
array1 = Name,Age,Allergy,Symptoms,Tested,State
array2 = Mihir,34,,Fever Sore throat,No,Rhode Island

zx8754
  • 52,746
  • 12
  • 114
  • 209
Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
  • I don't understand why people downvote the logical code related issue? and that person has downvoted my answer as well. Stackoverflow moderator should verify these kind of upvote and downvote – Mihir Oza May 04 '20 at 07:41

2 Answers2

1

well, you may iterate through items ,split each item by '=' and put keys and values in two different NSMUtableArray

but may be it will be better to use NSDictionary to hold key-values pairs

there may be another ways depending on what you are trying to do.

ish1313
  • 321
  • 3
  • 6
-1

Got my answer may be it's long way to get that but it works for me.

NSArray *items = [strCustomFields componentsSeparatedByString:@"&,"];
NSArray *subArr = nil;
NSMutableArray *arr1 = [[NSMutableArray alloc] init];
for (NSString* currentString in items) {
    subArr = [currentString componentsSeparatedByString:@"="];
    [arr1 addObject:[subArr objectAtIndex:0]];
    [arr1 addObject:[subArr objectAtIndex:1]];
    NSLog(@"subArr = %@", subArr);
}
NSLog(@"arr1 = %@", arr1);  
for (int i = 0; i< [arr1 count]/ 2; i++) {  
            myCustomView.lbltitle.text = [NSString stringWithFormat:@"%@", [arr1 objectAtIndex:(i *2)]];
            myCustomView.lblvalue.text = [NSString stringWithFormat:@"%@", [arr1 objectAtIndex:(i * 2) + 1]];
        }
Mihir Oza
  • 2,768
  • 3
  • 35
  • 61