gpt4 book ai didi

objective-c - Objective-C 中的 Sscanf 等价物

转载 作者:太空狗 更新时间:2023-10-30 03:28:25 25 4
gpt4 key购买 nike

我目前正在用 Objective-C 编写一个 wavefront OBJ 加载器,我正在尝试弄清楚如何以与 C 中的 sscanf() 函数类似的方式解析来自 NSString 的数据。

OBJ 文件以 x、y、z 三元组顶点、纹理坐标和法线定义面,例如:

f 1.43//2.43 1.11//2.33 3.14//0.009

目前我不关心纹理坐标。在 C 中,解析此行的一种简单方法是这样的语句:

sscanf(buf, "f %d//%d %d//%d %d//%d", &temp[0], &temp[1], &temp[2], &temp[3], &temp[4], &temp[5]);

显然,NSStrings 不能在 sscanf() 中使用,除非先将它们转换为 C 风格的字符串,但我想知道是否有更优雅的方式来执行此操作而不进行此类转换。

最佳答案

NSScanner类可以解析字符串中的数字,尽管它不能用作 sscanf 的直接替代品。

编辑:这是一种使用方法。您也可以将 / 放入要跳过的字符列表中。

float temp[6];
NSString *objContent = @"f 1.43//2.43 1.11//2.33 3.14//0.009";
NSScanner *objScanner = [NSScanner scannerWithString:objContent];

// Skip the first character.
[objScanner scanString:@"f " intoString:nil];

// Read the numbers.
NSInteger index=0;
BOOL parsed=YES;
while ((index<6)&&(parsed))
{
parsed=[objScanner scanFloat:&temp[index]];
// Skip the slashes.
[objScanner scanString:@"//" intoString:nil];

NSLog(@"Parsed %f", temp[index]);
index++;
}

关于objective-c - Objective-C 中的 Sscanf 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099612/

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