我正在尝试在 iOS 应用程序中提取字符串中的值。我有一个格式为 NSString:
地点:32 3203 292 293 321
我想将所有五个整数存储到 5 个不同的 int 变量中。整数具有不同的长度。我目前正在尝试确定使用空格字符作为指导的整数,但我遇到了问题。必须有一个更简单的方法来做到这一点。
这是我所拥有的:
int length = [locationString length];
NSMutableString * xStr, * yStr,* zStr, *tiltStr, *panStr;
int charIndex = -1;
unichar cur;
for(i = 3; i < length; i++)
{
cur = [locationString characterAtIndex:i];
if(cur == ' ') charIndex++;
else
{
if(charIndex == 0)
{
[xStr appendString:[NSString stringWithCharacters:&cur length:1]];
}
else if(charIndex == 1)
{
[yStr appendString:[NSString stringWithCharacters:&cur length:1]];
}
else if(charIndex == 2)
{
[zStr appendString:[NSString stringWithCharacters:&cur length:1]];
}
else if(charIndex == 3)
{
[tiltStr appendString:[NSString stringWithCharacters:&cur length:1]];
}
else if(charIndex == 4)
{
[panStr appendString:[NSString stringWithCharacters:&cur length:1]];
}
}
}
NSLog(@"x String: %@", xStr);
NSLog(@"y String: %@", yStr);
NSLog(@"z String: %@", zStr);
NSLog(@"tilt String: %@", tiltStr);
NSLog(@"pan String: %@", panStr);
if(xStr != NULL) xVal = [xStr integerValue];
if(yStr != NULL) yVal = [yStr integerValue];
if(zStr != NULL) zVal = [zStr integerValue];
if(tiltStr != NULL) tiltVal = [tiltStr integerValue];
if(panStr != NULL) panVal = [panStr integerValue];
但我每次都得到空字符串。其他人以前做过这样的事情吗?
谢谢