2012-03-02 50 views
0

我试图从服务器上的CSV文件中读取数据。该文件具有非常大量的数据。目前,我读使用的NSURLRequest该文件如下图所示:从服务器上的csv文件导入数据到核心数据

-(void)serverConnect{ 
     NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://spider90.dyndns.org:8080/sid/test.csv"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0]; 

     NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    } 

-(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{ 

     response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

     NSString *stripped1 = [response stringByReplacingOccurrencesOfString:@"\r" withString:@""]; 

     NSMutableArray *rows = [NSMutableArray arrayWithArray:[stripped1 componentsSeparatedByString:@"\n"]]; 
     NSMutableArray *contentArray = [NSMutableArray array]; 
     NSMutableArray *contentArray1 = [NSMutableArray array]; 
     NSArray *components; 
     NSString *startpoint; 

     for (int i=0;i<[rows count]; i++) { 

      if(i == 0 || [[rows objectAtIndex:i] isEqualToString:@""]){ 
       continue; 
      } 
      components = [[rows objectAtIndex:i] componentsSeparatedByString:@","]; 

     if(i == 1){ 

      startpoint = [components objectAtIndex:0]; 

      NSLog(@"startpoint:%@",startpoint); 

     } 


     id x = [components objectAtIndex:0] ; 
     id y = [components objectAtIndex:1]; 
     id z = [components objectAtIndex:2]; 

     [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"X",y,@"Y", nil]]; 
     [contentArray1 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"X",z,@"Y", nil]]; 
     NSLog(@"Contents of Uterus Contraction: %@",contentArray); 
     NSLog(@"Contents of Heart Beat: %@",contentArray1); 

} 
     [_graphHostingView setContentSize:CGSizeMake(2000, 1000)]; 
     _graphHostingView.scrollEnabled = YES; 
     _graphHostingView.showsHorizontalScrollIndicator = YES; 
     _graphHostingView.showsVerticalScrollIndicator = YES; 
     _graphHostingView.userInteractionEnabled = YES; 

     self.scatterPlot = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:contentArray andString:startpoint]; 
     self.scatterPlot1 = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:contentArray1 andString:startpoint]; 
     [self.scatterPlot plot1]; 
     [self.scatterPlot1 plot2]; 

} 

当我试图读取这个文件,我结束了在此错误:终止应用程序由于未捕获的异常“NSRangeException”,原因是:“* - [NSMutableArray objectAtIndex:]:index 1 beyond bounds [0 .. 0]'。

我被建议尝试使用核心数据来存储文件中的数据。问题在于文件。它正在实时更新,我需要回到之前从文件添加到数据库中的值。有没有什么建议可以指导我寻找什么?数据范围大约为21K加上数据行。 http://imgur.com/UluaC - 数据的样本截图。

回答

0

我没有足够的声望点来评论,但我猜一个或许多行是空的,所以尽管组件是NSArray而不是NSMutableArray,文件中的空白行导致了问题。