views:

178

answers:

3

Hi all,

I have a NSString: @"1a,1b,1c,1d,5c". I want this NSString separated into a NSMutableArray, but I don't know how. I think it is fairly simple but I can't find it (maybe because my English isn't good enough to find a good description for it to search on).

Regards, Dodo

A: 
[NSMutableArray arrayWithArray:[string componentsSeparatedByString:@","]];
Paul Lynch
+1  A: 

Use -componentsSeparatedByString: to explode.

The returned value is an NSArray. If you need an NSMutableArray, call the -mutableCopy method on it.

KennyTM
+2  A: 
NSString *_stringToSplit = @"1a,1b,1c,1d,5c";
NSArray *_splitItems = [_stringToSplit componentsSeparatedByString:@","];
NSMutableArray *_mutableSplitItems = [NSMutableArray arrayWithCapacity:[_splitItems count]]; 
[_mutableSplitItems addObjectsFromArray:_splitItems];
Alex Reynolds
What's the reason for the underlines? (Just wondering, I haven't seen that for local variables yet.)
Georg
It's just a visual indicator I use for quickly identifying local or short-lived, tightly-scoped variables.
Alex Reynolds