导入csv数据(SDK iphone) [英] Import csv data (SDK iphone)

查看:105
本文介绍了导入csv数据(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;

然后,我尝试从CSV文件加载数据,Data.csv文件中的数据相同的值和格式:

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. 



我运行代码,它应该给出相同的图形输出。但是,似乎没有从CSV文件成功加载数据。

我无法弄清楚我的代码有什么问题。

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;

}

唯一的区别是

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

推荐答案

我设置了一个示例项目,并尝试这个代码,它工作。

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

错误的两个最可能的原因是

The two most probable points of error are






p>我建议添加:

I would suggest adding:

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

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

并更改:


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


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

,然后添加:

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.

这篇关于导入csv数据(SDK iphone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆