sscanf的等效在Objective-C [英] Sscanf Equivalent in Objective-C

查看:233
本文介绍了sscanf的等效在Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个波前的OBJ装载机在Objective-C,我试图找出如何从一个NSString以类似的方式分析数据,在C的sscanf()函数。

I'm currently writing a wavefront OBJ loader in Objective-C and I'm trying to figure out how to parse data from an NSString in a similar manner to the sscanf() function in C.

OBJ文件中定义的面孔在X,Y,顶点的Z-三胞胎,纹理坐标和法线如:

OBJ files define faces in x,y,z triplets of vertices, texture coordinates, and normals such as:

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

我不关心纹理坐标在当前时刻。在C语言中,一个简单的方法来分析这条线将是一条语句,如:

I'm not concerned about texture coordinates at the current moment. In C, an easy way to parse this line would be a statement such as:

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

显然,NS​​String的不能用在的sscanf()没有首先将它们转换为C风格字符串,但我不知道是否有一个更优雅的方式来进行这种没有做这样的转换。

Obviously, NSStrings can't be used in a sscanf() without first converting them to a C-style string, but I'm wondering if there's a more elegant way to performing this without doing such a conversion.

推荐答案

的<一个href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html">NSScanner类可以在串解析数字,虽然不能被用作下拉更换为sscanf的

The NSScanner class can parse numbers in strings, although it can't be used as a drop-in replacement for sscanf.

修改:这是使用它的方法之一。你也可以把 / 成字符被跳过列表。

Edit: here's one way to use it. You can also put the / into the list of characters to be skipped.

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++;
}

这篇关于sscanf的等效在Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆