views:

506

answers:

1

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!!!!

+2  A: 
ericgorr
thanks for your help!!!yeah, i tracked the location of the data file and make sure i am getting the right path. it works!!
Ni
Great. Don't forget to mark this question as answered.
ericgorr