我是新编码所以请原谅我,如果这看起来像一个简单的问题.
我正在尝试在地图上绘制坐标.
我想读取CSV文件并将信息传递给两个单独的数组.
第一个数组将是NSArray*towerInfo(包含纬度,经度和塔标题)
第二个,NSArray*区域(包含塔标题和区域)与第一个数组具有相同的计数索引.
基本上,我相信我需要;
1)将文件读取为字符串.....
2)将字符串分成每个/ n/r分隔的临时数组......
3)循环访问临时数组并在每次将此信息附加到两个主存储阵列之前创建塔和区域对象.
这是正确的过程,如果有的话,那里有人可以发布一些示例代码,因为我真的很难做到这一点.
感谢所有提前.
克里斯.
我编辑了这个以显示我的代码示例.我遇到的问题是我收到警告说
1)"'dataStr'的本地声明隐藏了实例变量.2)"'array'的本地声明隐藏了实例变量.
我想我明白这些意思,但我不知道怎么解决它.程序编译并运行但日志告诉我"数组为空".
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize dataStr;
@synthesize array;
-(IBAction)convert {
//calls the following program when the onscreen 'convert' button is pressed.
NSString *dataStr = [NSString stringWithContentsOfFile:@"Towers.csv" encoding:NSUTF8StringEncoding error:nil];
//specifies the csv file to read - stored in project root directory - and encodes specifies that the format of the file is NSUTF8. Choses not to return an error message if the reading fails
NSArray *array = [dataStr componentsSeparatedByString: @","];
//splits the string into an array by identifying data separators.
NSLog(@"array: %@", array);
//prints the array to screen
}
Run Code Online (Sandbox Code Playgroud)
任何额外的帮助将不胜感激.感谢到目前为止的回复.
NSString* fileContents = [NSString stringWithContentsOfURL:filename ...];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (...
NSString* row = [rows objectAtIndex:n];
NSArray* columns = [row componentsSeparatedByString:@","];
...
Run Code Online (Sandbox Code Playgroud)
您可能希望抛出一些"stringTrimmingCharactersInSet"调用来修剪空格.
| 归档时间: |
|
| 查看次数: |
13871 次 |
| 最近记录: |