gpt4 book ai didi

ios - ios 查找字符串中的值

转载 作者:行者123 更新时间:2023-11-29 04:16:05 25 4
gpt4 key购买 nike

我正在尝试在 iOS 应用程序中提取字符串中的值。我有一个 NSString 格式:

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];

但我每次都得到空字符串。其他人以前做过类似的事情吗?

谢谢

最佳答案

我认为你应该考虑使用 NSString componentsSeparatedByString API 让您的生活更轻松。您将使用 [locationString ComponentsSeparatedByString:@""] 获得一个 NSArray 字符串,并且您的整数将以 1-5 为单位

关于ios - ios 查找字符串中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com