开发者

Import csv data (SDK iphone)

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 succes开发者_JAVA技巧sfully.

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜