NSScanner 行为
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是
NSScanner
的工作方式。当您使用scanUpToCharactersFromSet:intoString:
时,它会扫描最多但不包括集合中的字符。如果您希望它移动过去字符集中的字符,您有两个选择:让它扫描这些字符。您现在正在使用
scanCharactersFromSet:intoString:
执行此操作。另一种方法是[fields scanString:self.separator intoString:nil]
。使用
setCharactersToBeSkipped:
告诉扫描器要跳过分隔符。但是,这将使您很难检测到空字段。扫描仪默认要跳过的字符集包括换行符。这就是为什么你的外部扫描仪会跳过换行符。
您可以完全使用
componentsSeparatedByString:
来完成此操作,而不是使用NSScanner
。例子:That's just the way
NSScanner
works. When you usescanUpToCharactersFromSet: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: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]
.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 usingNSScanner
. Example: