0

I have this string a:10,b:xx,e:20,m:xy,w:30,z:50

But, the keys and the value change...

I want to convert it to a structure in objective-c (I want the app to convert the string to structure automatically, not manually)

In PHP the result will be:

 $array = array( 'a' => 10, // or 'a' => "10" (doesn't matter) 'b' => "xx", 'e' => 20, // or 'e' => "20" (doesn't matter) 'm' => "xy", 'w' => 30, // or 'w' => "30" (doesn't matter) 'z' => 50 // or 'z' => "50" (doesn't matter) ); 

So, how can I convert that string to structure and get the result like in the PHP example but to do it in objective-c... ?

Please show me the exact code, I'm new to objective-c :)

Thank you

3 Answers 3

2

That PHP structure looks more like a dictionary to me, so I'm going to assume it is.

NSString *s = @"a:10,b:xx,e:20,m:xy,w:30,z:50"; NSArray *pairs = [s componentsSeparatedByString:@","]; NSMutableArray *keys = [NSMutableArray array]; NSMutableArray *values = [NSMutableArray array]; for (NSString *pair in pairs) { NSArray *keyValue = [pair componentsSeparatedByString:@":"]; [keys addObject:[keyValue objectAtIndex:0]]; [values addObject:[keyValue objectAtIndex:1]]; } NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSLog(@"%@", dict); 

That is obviously pretty verbose, but it requires some fancy string manipulation.

Output looks like:

{ a = 10; b = xx; e = 20; m = xy; w = 30; z = 50; } 
Sign up to request clarification or add additional context in comments.

4 Comments

that's why I hate Objective-C. For everyone coming from PHP it's really a pain
String manipulation isn't Objective-C's strong point (other then the fact it got Unicode support much better than most alternatives). Going to any language will be a 'pain.' But its about using the right tool for the job. When on iOS though, its the only (sane) tool for most jobs.
It's not only the string. To do almost everything you need to be so verbous that if you are not used to you will get mad soon
I respectfully disagree as the verbosity aids with readability and maintainability. This isn't the place for a language war though.
1

You can explode the string, explode the fragments again and put all that into a dictionary. This should work:

NSMutableDictionary *dict = [NSMutableDictionary dictionary]; NSString *str = @"a:10,b:xx,e:20,m:xy,w:30,z:50"; NSArray *arr = [str componentsSeparatedByString:@","]; for (NSString *fragment in arr) { NSArray *keyvalue = [fragment componentsSeparatedByString:@":"]; [dict setObject:[keyvalue objectAtIndex:0] forKey:[keyvalue objectAtIndex:1]]; } 

Comments

1
NSString* myString = @"a:10,b:xx,e:20,m:xy,w:30,z:50"; 

Use below method of NSString.

- (NSArray *)componentsSeparatedByString:(NSString *)separator 

NSArray* myArray = [myString componentsSeparatedByString:@","]; NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary]; for (NSString* stringObj in myArray) { NSArray *myTemp1 = [stringObj componentsSeparatedByString:@":"]; [myDictionary setObject:[myTemp1 objectAtIndex:0] forKey:[myTemp1 objectAtIndex:1]]; } 

Now myDictionary instance is equivalent to $array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.