【问题标题】:Parsing a .csv file from a server with Objective-C使用 Objective-C 从服务器解析 .csv 文件
【发布时间】:2012-07-12 10:49:08
【问题描述】:

找了好久还是没找到,所以想自己来问。

在我的 iPad 应用程序中,我需要能够解析 .csv 文件才能填充表格。我正在使用 http://michael.stapelberg.de/cCSVParse 来解析 csv 文件。但是,我只成功解析了本地文件。我一直在尝试从服务器访问文件,但无处可去。

这是我解析本地 .csv 文件的代码:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        //UITextField *reply = [alertView textFieldAtIndex:buttonIndex];
        NSString *fileName = input.text;
        NSLog(@"fileName %@", fileName);
        CSVParser *parser = [CSVParser new];
        if ([fileName length] != 0)
        {
            NSString *pathAsString = [[NSBundle mainBundle]pathForResource:fileName ofType:@"csv"];
            NSLog(@"%@", pathAsString);

            if (pathAsString != nil)
            {
                [parser openFile:pathAsString];
                NSMutableArray *csvContent = [parser parseFile];
                NSLog(@"%@", csvContent);
                [parser closeFile];
                NSMutableArray *heading = [csvContent objectAtIndex:0];
                [csvContent removeObjectAtIndex:0];

                NSLog(@"%@", heading);

                AppDelegate *ap = [AppDelegate sharedAppDelegate];
                NSManagedObjectContext *context = [ap managedObjectContext];

                NSString *currentHeader = [heading objectAtIndex:0];
                NSString *currentValueInfo = [heading objectAtIndex:1];
                NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Field" inManagedObjectContext:context];
                [newObject setValue:@"MIS" forKey:@"header"];
                [newObject setValue:currentHeader forKey:@"fieldName"];

                for (NSArray *current in csvContent)
                {
                    NSManagedObject *newField = [NSEntityDescription insertNewObjectForEntityForName:@"Field" inManagedObjectContext:context];
                    [newField setValue:currentHeader forKey:@"header"];
                    [newField setValue:currentValueInfo forKey:@"valueInfo"];
                    NSLog(@"%@", [current objectAtIndex:0]);
                    [newField setValue:[current objectAtIndex:0] forKey:@"fieldName"];
                    [newField setValue:[NSNumber numberWithDouble:[[current objectAtIndex:1] doubleValue]] forKey:@"value"];
                }
                NSError *error;
                if (![context save:&error]) 
                {
                    NSLog(@"Couldn't save: %@", [error localizedDescription]);
                }
                [self storeArray];
                [self.tableView reloadData];
            }
        }
    }
    input.text = nil;
}

请原谅奇怪的开始和结束括号缩进。 :/

无论如何,这就是我从用户那里获取输入并在本地访问文件的代码,我相信你们已经意识到了这一点。现在我想知道如何在我的服务器中获取文件的路径。

另外,如果你们发现任何其他错误,例如写作风格和其他不良习惯,请告诉我,因为我是 iOS 新手。

提前非常感谢您!如果您不理解我的问题,请澄清,因为我有时不善于解释自己! :)

【问题讨论】:

  • 你的问题不清楚,你有服务器吗?你把你的csv文件放在服务器上吗?你知道怎么做吗?
  • 在我看来,这里涉及到几个步骤 - 1. 如何从服务器获取文件 2. 如何将该文件保存在本地(内存或磁盘) 3. 如何获取文件进入解析器。 4. 显示它。这些步骤中的哪一个给您带来麻烦?看来你已经控制了最后两个,对吗?
  • @Monolo 你是绝对正确的。涉及几个步骤。是的,我控制了最后两个。我遇到的问题是获取文件的路径。我想我可以将文件存储在本地,之后获取路径名应该不是问题。
  • @samirFrance 是的,我有一台服务器,并且 .csv 文件在服务器中。我的问题是我不知道如何获取文件。

标签: objective-c xcode ipad ios5 csv


【解决方案1】:

我猜你正试图从服务器的 .csv 文件中获取数据,并希望在表格视图列表中显示该数据。

所以我建议您尝试在 NSData 中获取该 .csv 文件数据,然后进行处理。

NSData *responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"serverUrl"]];
NSString *csvResponseString = [[[NSString alloc] initWithData:responseData   encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"responseString--->%@",csvResponseString);

现在尝试使用 nsstring 的方法 (componentsSeparatedByString) 和逗号 (')

arrSepratedData = [[responseString componentsSeparatedByString:@","];

现在使用此 arr 填充 UITableView 数据。

【讨论】:

  • 嗨!我已经尝试过你的方法并且它有效。但我的问题是我需要一个文件路径。只有这样解析器才能使用它。我需要通过我的应用程序保持统一。因此,由于构建问题,我无法使用您的方法。 :(
  • 能否请您告诉我您的申请流程。可能对我和其他人有帮助。并解释是否有必要将数据保存在本地数据库/Coredata 中。如果可能,发送 .csv 文件数据将有助于理解..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 2023-04-07
相关资源
最近更新 更多