我試圖在iOS應用程序中取消字符串中的值。我的格式爲一個NSString:在字符串中查找值ios
LOC: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];
但每次我得到空字符串。任何人都做過這樣的事情?
感謝