NSScanner 行为

发布于 2025-01-06 18:03:58 字数 1610 浏览 0 评论 0原文

我对 iOS 开发非常陌生。我正在尝试解析一个简单的 csv 文件,该文件包含大约 10 行以逗号分隔的行。我正在使用下面的代码,但无法理解为什么 NSScanner 在解析字段(下面代码中的字段)时不会转到逗号后的下一个字符串。我必须执行该行

[fields scanCharactersFromSet:fieldCharSet intoString:nil];

以使其超过分隔符。但是,我不必对行执行相同的操作 - NSScanner 自动将位置设置为换行符之后的下一行。在这两种情况下,我都使用相同的方法 - [lines scanUpToCharactersFromSet:intoString] 有什么我不明白的吗?

这是我试图解析的测试文件:

Name,Location,Number,Units
A,AA,4,mm
B,BB,3.5,km
C,CC,10.2,mi
D,DD,2,mm
E,EE,6,in
F,FF,2.8,m
G,GG,3.7,km
H,HH,4.3,mm
I,II,4,km

这是我的代码:

-(void)parseFile {
    NSCharacterSet *lineCharSet = [NSCharacterSet newlineCharacterSet];
    NSCharacterSet *fieldCharSet = [NSCharacterSet characterSetWithCharactersInString:self.separator];
    // import the file

    NSStringEncoding *encoding = nil;
    NSError *error = nil;
    NSString *data = [[NSString alloc] initWithContentsOfURL:self.absoluteURL usedEncoding:encoding error:&error];
    NSString *line,*field;


    NSScanner *lines = [NSScanner scannerWithString:data]; 
    while (![lines isAtEnd]) {

        [lines scanUpToCharactersFromSet:lineCharSet intoString:&line];//automatically sets to next line - why?



        NSLog(@"%@\n",line);

        NSScanner *fields = [NSScanner scannerWithString:line];

        while (![fields isAtEnd]) {

            [fields scanUpToCharactersFromSet:fieldCharSet intoString:&field];

            [fields scanCharactersFromSet:fieldCharSet intoString:nil]; //have to do this otherwise will not go to next symbol

            NSLog(@"%@\n", field);

        }
    }
}

I am very new to iOS development. I am trying to parse a simple csv file that has about 10 lines separated by commas. I am using the code below but not able understand why NSScanner, when parsing the fields (fields in the code below) does not go to the next string after the comma. I have to execute the line

[fields scanCharactersFromSet:fieldCharSet intoString:nil];

to make it go past the delimiter. However, I don't have to do the same thing for lines - NSScanner automatically sets the position to the next line past the newline. In both cases I am using the same method - [lines scanUpToCharactersFromSet:intoString] Is there something I am not understanding?

Here is the test file I am trying to parse:

Name,Location,Number,Units
A,AA,4,mm
B,BB,3.5,km
C,CC,10.2,mi
D,DD,2,mm
E,EE,6,in
F,FF,2.8,m
G,GG,3.7,km
H,HH,4.3,mm
I,II,4,km

Here is my code:

-(void)parseFile {
    NSCharacterSet *lineCharSet = [NSCharacterSet newlineCharacterSet];
    NSCharacterSet *fieldCharSet = [NSCharacterSet characterSetWithCharactersInString:self.separator];
    // import the file

    NSStringEncoding *encoding = nil;
    NSError *error = nil;
    NSString *data = [[NSString alloc] initWithContentsOfURL:self.absoluteURL usedEncoding:encoding error:&error];
    NSString *line,*field;


    NSScanner *lines = [NSScanner scannerWithString:data]; 
    while (![lines isAtEnd]) {

        [lines scanUpToCharactersFromSet:lineCharSet intoString:&line];//automatically sets to next line - why?



        NSLog(@"%@\n",line);

        NSScanner *fields = [NSScanner scannerWithString:line];

        while (![fields isAtEnd]) {

            [fields scanUpToCharactersFromSet:fieldCharSet intoString:&field];

            [fields scanCharactersFromSet:fieldCharSet intoString:nil]; //have to do this otherwise will not go to next symbol

            NSLog(@"%@\n", field);

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风追烟花雨 2025-01-13 18:03:58

这就是 NSScanner 的工作方式。当您使用 scanUpToCharactersFromSet:intoString: 时,它会扫描最多但不包括集合中的字符。如果您希望它移动过去字符集中的字符,您有两个选择:

  1. 让它扫描这些字符。您现在正在使用 scanCharactersFromSet:intoString: 执行此操作。另一种方法是[fields scanString:self.separator intoString:nil]

  2. 使用setCharactersToBeSkipped:告诉扫描器要跳过分隔符。但是,这将使您很难检测到空字段。

扫描仪默认要跳过的字符集包括换行符。这就是为什么你的外部扫描仪会跳过换行符。

您可以完全使用 componentsSeparatedByString: 来完成此操作,而不是使用 NSScanner。例子:

-(void)parseFile {
    NSString *data = [[NSString alloc] initWithContentsOfURL:self.absoluteURL usedEncoding:encoding error:&error];

    for (NSString *line in [data componentsSeparatedByString:@"\n"]) {
        if (line.length == 0)
            continue;

        NSLog(@"line: %@", line);

        for (NSString *field in [line componentsSeparatedByString:self.separator]) {
            NSLog(@"    field: %@", field);
        }
    }
}

That's just the way NSScanner works. When you use scanUpToCharactersFromSet:intoString:, it scans characters up to but not including the characters in the set. If you want it to move past characters in the set, you have two options:

  1. Make it scan those characters. You are doing this now using scanCharactersFromSet:intoString:. Another way you could do it is [fields scanString:self.separator intoString:nil].

  2. Tell the scanner that the separator character is to be skipped, using setCharactersToBeSkipped:. However, this will make it hard for you to detect empty fields.

The scanner's default set of characters-to-be-skipped includes the newline. That's why your outer scanner skips the newline.

You could do this entirely using componentsSeparatedByString:, instead of using NSScanner. Example:

-(void)parseFile {
    NSString *data = [[NSString alloc] initWithContentsOfURL:self.absoluteURL usedEncoding:encoding error:&error];

    for (NSString *line in [data componentsSeparatedByString:@"\n"]) {
        if (line.length == 0)
            continue;

        NSLog(@"line: %@", line);

        for (NSString *field in [line componentsSeparatedByString:self.separator]) {
            NSLog(@"    field: %@", field);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文