Import csv data (SDK iphone)

流过昼夜 提交于 2019-12-31 04:06:08

问题


For the following code, I can read all the data in the string, and successfully get the data for plot.

NSMutableArray *contentArray = [NSMutableArray array];
NSString *filePath = @"995,995,995,995,995,995,995,995,1000,997,995,994,992,993,992,989,988,987,990,993,989";
NSArray *myText = [filePath componentsSeparatedByString:@","];  
NSInteger idx;    
for (idx = 0; idx < myText.count; idx++) {
    NSString *data =[myText objectAtIndex:idx];
    NSLog(@"%@", data);
    id x = [NSNumber numberWithFloat:0+idx*0.002777778];
    id y = [NSDecimalNumber decimalNumberWithString:data];          
    [contentArray addObject:
    [NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];    
}

self.dataForPlot = contentArray;

then, I try to load the data from a CSV file, the data in Data.csv file has the same value and the same format as:

995,995,995,995,995,995,995,995,1000,997,995,994,992,993,992,989,988,987,990,993,989. 

I run the code, it is supposed to give the same graph output. however, it seems that the data is not loaded from the CSV file successfully.
I cannot figure out what's wrong with my code.

NSMutableArray *contentArray = [NSMutableArray array];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"csv"];
NSString *Data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];    
if (Data)
{
    NSArray *myText = [Data componentsSeparatedByString:@","];
    NSInteger idx;    
    for (idx = 0; idx < myText.count; idx++) {
        NSString *data =[myText objectAtIndex:idx];
                    NSLog(@"%@", data);
        id x = [NSNumber numberWithFloat:0+idx*0.002777778];
        id y = [NSDecimalNumber decimalNumberWithString:data];        
        [contentArray addObject:
        [NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y",nil]];    
    }
    self.dataForPlot = contentArray;

}

The only difference is

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"csv"];
NSString *Data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];   
if (data){
}

Did I do anything wrong here?


回答1:


I setup a sample project and tried this code and it worked.

The two most probable points of error are

  1. you aren't getting the file path (i.e. filePath is nil)
  2. you aren't reading the file correctly.

I would suggest adding:

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

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

and changing:

NSString *Data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];    

to

NSError*  error;
NSString* Data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error ];    

and then adding:

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

Of course, running this through the debugger and checking the return values should work as well and let you know exactly where it is failing.



来源:https://stackoverflow.com/questions/2444975/import-csv-data-sdk-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!