小编Ni.*_*Ni.的帖子

导入csv数据(SDK iphone)

对于以下代码,我可以读取字符串中的所有数据,并成功获取绘图数据.

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;
Run Code Online (Sandbox Code Playgroud)

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

995,995,995,995,995,995,995,995,1000,997,995,994,992,993,992,989,988,987,990,993,989. 
Run Code Online (Sandbox Code Playgroud)

我运行代码,它应该给出相同的图形输出.但是,似乎未成功从CSV文件加载数据.
我无法弄清楚我的代码有什么问题.

NSMutableArray *contentArray = [NSMutableArray array];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"csv"];
NSString *Data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];    
if (Data) …
Run Code Online (Sandbox Code Playgroud)

csv iphone cocoa-touch nsstring ios

1
推荐指数
1
解决办法
7125
查看次数

标签 统计

cocoa-touch ×1

csv ×1

ios ×1

iphone ×1

nsstring ×1