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?
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.
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.
精彩评论