NSScanner - 将下一行扫描到字符串中

时间:2022-01-28 19:59:48

I am working with NSScanner to parse data that is both comma and line delimited. It was fairly straightforward to implement, but I am running into a very simple issue. I would like to scan in the next line, even if it is empty. The problem I am having now is that if there is an empty line, it will skip it and read the next line in. Then my parsing is off because the wrong data ended up in the wrong place. Here is the code I have now:

我正在使用NSScanner来解析逗号和行分隔的数据。实现起来相当简单,但我遇到了一个非常简单的问题。我想扫描下一行,即使它是空的。我现在遇到的问题是,如果有一个空行,它将跳过它并读取下一行。然后我的解析关闭,因为错误的数据最终在错误的地方。这是我现在的代码:

NSCharacterSet *setToIgnore = [NSCharacterSet characterSetWithCharactersInString:@",\r\n"];
[scanner setCharactersToBeSkipped:setToIgnore];

NSInteger levelID, campaignID;
NSString *title, *about, *gameBoard;
BOOL aTest = [scanner scanInteger:&levelID] && [scanner scanInteger:&campaignID];
aTest = [scanner scanUpToString:@"\r\n" intoString:&title];
aTest = [scanner scanUpToString:@"\r\n" intoString:&about];
aTest = [scanner scanUpToString:@"\r\n" intoString:&gameBoard];

So the data is formatted like so: int,int string string string

所以数据的格式如下:int,int string string string

Sometimes, the second string (about) is empty, and NSScanner skips it and reads the gameBoard string into about. How do I tell NSScanner to read in the next line?

有时,第二个字符串(about)为空,NSScanner会跳过它并将gameBoard字符串读入about。如何告诉NSScanner在下一行中阅读?

2 个解决方案

#1


Solution 1: Remove the setToIgnore statements and scan past your delimiters manually:

解决方案1:删除setToIgnore语句并手动扫描分隔符:

NSInteger levelID, campaignID;
NSString *title, *about, *gameBoard;
BOOL aTest = [scanner scanInteger:&levelID]

[scanner setScanLocation:[scanner scanLocation]+1]; // skip comma
aTest=[scanner scanInteger:&campaignID];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break
aTest = [scanner scanUpToString:@"\r\n" intoString:&title];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break
aTest = [scanner scanUpToString:@"\r\n" intoString:&about];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break
aTest = [scanner scanUpToString:@"\r\n" intoString:&gameBoard];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break

Solution 2: Replace the whole lot with:

解决方案2:用以下内容替换整个批次:

NSArray *lines=[file componentsSeparatedByString:@"\r\n"];

And then loop through and parse the lines individually.

然后循环并分别解析这些行。

#2


Don't ignore \r and \n.

不要忽略\ r和\ n。

#1


Solution 1: Remove the setToIgnore statements and scan past your delimiters manually:

解决方案1:删除setToIgnore语句并手动扫描分隔符:

NSInteger levelID, campaignID;
NSString *title, *about, *gameBoard;
BOOL aTest = [scanner scanInteger:&levelID]

[scanner setScanLocation:[scanner scanLocation]+1]; // skip comma
aTest=[scanner scanInteger:&campaignID];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break
aTest = [scanner scanUpToString:@"\r\n" intoString:&title];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break
aTest = [scanner scanUpToString:@"\r\n" intoString:&about];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break
aTest = [scanner scanUpToString:@"\r\n" intoString:&gameBoard];
[scanner setScanLocation:[scanner scanLocation]+2]; // skip line break

Solution 2: Replace the whole lot with:

解决方案2:用以下内容替换整个批次:

NSArray *lines=[file componentsSeparatedByString:@"\r\n"];

And then loop through and parse the lines individually.

然后循环并分别解析这些行。

#2


Don't ignore \r and \n.

不要忽略\ r和\ n。