2012-03-02 71 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,文件中的空白行導致了問題。