【发布时间】: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