【问题标题】:Importing data into core data from a csv file on a server将数据从服务器上的 csv 文件导入核心数据
【发布时间】:2012-03-02 18:50:48
【问题描述】:

我正在尝试从服务器上的 CSV 文件中读取数据。这个文件有非常大量的数据。目前我正在使用 NSURLREQUEST 读取这个文件,如下所示:

 -(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 - 数据的示例截图。

【问题讨论】:

    标签: xcode parsing core-data csv


    【解决方案1】:

    我没有足够的声誉点来发表评论,但我猜其中一行或多行是空白的,因此尽管 components 是 NSArray 而不是 NSMutableArray,但文件中的空白行会导致问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多