2012-12-04 42 views
0

我试图在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]; 

但每次我得到空字符串。任何人都做过这样的事情?

感谢

回答

0

我会

[locationString componentsSeparatedByString:@" "]; 

此启动将给你一个与每个字符串组成的数组。

然后,您可以忽略包含@“loc:”的索引0,其余数组索引将包含您将使用的数据。

1

我想你应该考虑使用的NSString componentsSeparatedByString API,使您的生活更轻松。你会得到使用[locationString componentsSeparatedByString:@" "]字符串NSArray的,你的整数会在1-5的indeces