来自NSMutableURLRequest的CSV解析错过了前150行

时间:2012-03-06 19:58:38

标签: ios xcode csv core-plot

我正在使用.CSV在服务器上读取NSMutableURLRequest文件,当我对数据进行CSV解析时,我最终读取的绘图点不完整。例如,如果我有300多行,应用程序会从第150行加上行。关于如何处理此问题的任何建议?是否存在存储绘图点值的函数?对不起,我是iOS新手。非常感谢您的帮助

  - (id) init
{
// regular [super init], etc. etc.
   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(_connectionTimer:) userInfo:nil repeats:YES];
// other custom initialization continues
    return self;
}

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

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

         }


 - (void) _connectionTimer:(NSTimer *)timer {
           [self serverConnect];
       }

 -(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{
         response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     }

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {


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

         NSMutableArray *rows = [NSMutableArray arrayWithArray:[stripped1 componentsSeparatedByString:@"\n"]];
         NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:[rows count]]; 
         NSMutableArray *contentArray1 = [NSMutableArray arrayWithCapacity:[rows count]];
         NSArray *components;

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

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

         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]];
    }

}

- (void)viewDidLoad
 {
   [super viewDidLoad];
   [_graphHostingView setContentSize:CGSizeMake(2000, 1000)];
   _graphHostingView.scrollEnabled = YES;
   _graphHostingView.showsHorizontalScrollIndicator = YES;
   _graphHostingView.showsVerticalScrollIndicator = YES;



   [self init];
   //[self serverConnect];

}

变量响应为NSString。如果我使用MSMutableStringNSMutableArray会更好吗? 如果没有,任何人都可以解释-(NSArray *)numbersForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange;方法有何帮助?它能帮我保存情节数据吗?那么接下来我可以在核心图用于绘图时加载吗?使用计时器会影响正在读取的数据吗?

1 个答案:

答案 0 :(得分:0)

我会尝试使用stringWithContentsOfURL而不是您用来从服务器提取数据的方法。我认为这是问题发生的地方,你的最终目标是将所有数据都转换为字符串,这样做是一种更有效的方法。

相关问题