views:

42

answers:

2

I have a UITextField that has data in it separated by commas (i.e, 1,2,4) I want to look at this string and extract all the numbers out of it that are separated by the commas and put it in and array. So in this example 1 2 4 would be stored in an array. Can someone help me code something like this?

+1  A: 

Sure, no problem.

NSString *s = @"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:@","];

Now you have an array of NSString objects - something like { @"1", @"2", @"4" }. You can convert those to NSNumbers or to regular integer types if you like. Here's a link to the NSString documentation for your reference.

Carl Norum
A: 

If there's nothing else in your string of commas and digits, then you can use something like [string componentsSeparatedByString:@","]. Then you'd just need to reiterate over returned array and type-cast each element. If there's some sort of "info-noise" you may want to look into NSScanner class reference.

Eimantas