NSScanner 不导入数据

发布于 2024-09-25 03:08:39 字数 1063 浏览 3 评论 0原文

我正在尝试将 csv 文件中的一组默认数据加载到我的核心数据数据库中。因此,最初我尝试读取 csv 文件并将其输出到日志,然后再尝试将其添加到核心数据数据库。

这是我正在使用的代码;

//Find import file;

NSString *defaultCSVPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"csv"];

//Get the data into a string

NSString *fileString = [NSString stringWithContentsOfFile:defaultCSVPath encoding:NSUTF8StringEncoding error:nil];

if ( nil == fileString ) {
    NSLog(@"Could not open file data.csv");
    abort();
}
// Create the scanner
NSScanner *scanner = [NSScanner scannerWithString:fileString];

// Ignore new lines
[scanner setCharactersToBeSkipped:
 [NSCharacterSet characterSetWithCharactersInString:@"\n"]];

NSString *sNumber = @"";
NSString *sTitle = @"";

//Gets to here and this expression is never TRUE
while ( [scanner scanString:@"," intoString:&sNumber] && [scanner scanString:@"," intoString:&sTitle]) {
    NSLog(@"sNumber:%@ sTitle:%@",sNumber,sTitle);
}

我正在使用的示例数据是;

A15Q,Test1
F74443AAZ,Test2

当我跟踪代码时,我到达 while 子句,它只是跳过它。

I'm trying to load a default set of data from a csv file into my core data db. So initially i'm trying to read in a csv file and output it to the log before trying to add it to the core data database.

This is the code i'm using;

//Find import file;

NSString *defaultCSVPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"csv"];

//Get the data into a string

NSString *fileString = [NSString stringWithContentsOfFile:defaultCSVPath encoding:NSUTF8StringEncoding error:nil];

if ( nil == fileString ) {
    NSLog(@"Could not open file data.csv");
    abort();
}
// Create the scanner
NSScanner *scanner = [NSScanner scannerWithString:fileString];

// Ignore new lines
[scanner setCharactersToBeSkipped:
 [NSCharacterSet characterSetWithCharactersInString:@"\n"]];

NSString *sNumber = @"";
NSString *sTitle = @"";

//Gets to here and this expression is never TRUE
while ( [scanner scanString:@"," intoString:&sNumber] && [scanner scanString:@"," intoString:&sTitle]) {
    NSLog(@"sNumber:%@ sTitle:%@",sNumber,sTitle);
}

The sample data i'm using is;

A15Q,Test1
F74443AAZ,Test2

When I trace the code, I get to the while clause and it just skips over it.

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

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

发布评论

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

评论(1

︶葆Ⅱㄣ 2024-10-02 03:08:39

不知道为什么将 sNumber 和 sTitle 初始化为@""; nil 是更好的默认值(或者你可以不初始化它们)。

[scanner scanString:@"," intoString:&foo] 期望看到逗号并返回 foo 中的逗号(这样做是为了保持一致性,并且(我认为) Unicode wotsits)。

您可能想做类似这样的事情:

  1. 阅读逗号之前的所有内容。
  2. 跳过逗号。
  3. 阅读换行符之前的所有内容。
  4. 跳过换行符。

(出于某种原因,我需要在此处粘贴一些内容以使格式正常工作。)

while (
     [scanner scanUpToStringString:@"," intoString:&sNumber]
  && [scanner scanString:@"," intoString:NULL]
  && [scanner scanUpToString:@"\n" intoString:&sTitle]
  && [scanner scanString:@"\n" intoString:NULL]
) {
  NSLog(@"sNumber:%@ sTitle:%@",sNumber,sTitle);
}

假设您正在尝试解析 CSV,这有其自身的问题:

  • 您可能希望将 \r\n 视为换行符。
  • 它不能干净地处理空白行或没有逗号的行。
  • 它不处理带引号的字符串(例如"a,b","c,d,e,f")。

Not sure why you initialize sNumber and sTitle to @""; nil is a better default (or you can just not initialize them).

[scanner scanString:@"," intoString:&foo] expects to see a comma and returns the comma in foo (it does this for consistency and (I think) Unicode wotsits).

You probably want to do something more like this:

  1. Read everything before the comma.
  2. Skip over the comma.
  3. Read everything before the newline.
  4. Skip over the newline.

(And I need to stick something here for the formatting to work, for some reason.)

while (
     [scanner scanUpToStringString:@"," intoString:&sNumber]
  && [scanner scanString:@"," intoString:NULL]
  && [scanner scanUpToString:@"\n" intoString:&sTitle]
  && [scanner scanString:@"\n" intoString:NULL]
) {
  NSLog(@"sNumber:%@ sTitle:%@",sNumber,sTitle);
}

Assuming you're trying to parse CSV, this has its own problems:

  • You probably want to treat \r\n as a newline.
  • It doesn't cleanly handle blank lines or lines without a comma.
  • It doesn't handle quoted strings (e.g. "a,b","c,d,e,f").
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文