Import csv data (SDK iphone)
- by Ni
I am new to cocoa. I have been working on these stuff for a few days.
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 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 csv file successfully.
i can not 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??
Thanks for your help!!!!