我正在以这种格式从服务器接收字符串:
0_1_2_3
我的任务是从这个字符串中选择数字来填充四个标签。第一个想法是:
NSString *res1 = [result substringWithRange:NSMakeRange(0, 1)];
[firstLabel setText:res1];
用适当的标签四次。但是操作会重复很多次,每次我都会收到一个数字值增加的字符串。因此,当每个数字都是小数时,此代码将不起作用。那么我怎样才能以适当的方式独立于它们的长度跟踪每个数字呢?
我正在以这种格式从服务器接收字符串:
0_1_2_3
我的任务是从这个字符串中选择数字来填充四个标签。第一个想法是:
NSString *res1 = [result substringWithRange:NSMakeRange(0, 1)];
[firstLabel setText:res1];
用适当的标签四次。但是操作会重复很多次,每次我都会收到一个数字值增加的字符串。因此,当每个数字都是小数时,此代码将不起作用。那么我怎样才能以适当的方式独立于它们的长度跟踪每个数字呢?
NSString
附带一个方便的方法,称为-componentsSeparatedByString:
NSString *myString = @"0_1_2_3";
NSArray *myDigitStrings = [myString componentsSeparatedByString:@"_"];
/* access digit strings from myDigitStrings array by index or fast enumeration... */
for (NSString *myDigitString in myDigitStrings)
NSLog(@"digit string: %@", myDigitString);