【发布时间】:2012-04-18 16:47:35
【问题描述】:
我是编码新手,所以如果这似乎是一个简单的问题,请原谅。
我正在尝试在地图上绘制坐标。
我想读取一个 CSV 文件并将信息传递给两个单独的数组。
第一个数组是 NSArray *towerInfo(包含纬度、经度和塔名)
第二个,NSArray *region(包含塔标题和区域),与第一个数组具有相同的计数索引。
基本上,我认为我需要这样做;
1) 将文件读入字符串.....
2) 将字符串分割成一个临时数组,每个/n/r......
3) 循环遍历 temp 数组,每次都创建一个 tower 和 region 对象,然后将此信息附加到两个主存储数组。
这是正确的过程吗?如果是的话,有没有人可以发布一些示例代码,因为我真的很难做到这一点。
在此先感谢大家。
克里斯。
我对此进行了编辑以显示我的代码示例。我遇到的问题是我收到警告说
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
}
任何额外的帮助将不胜感激。感谢您迄今为止的回复。
【问题讨论】:
-
首先你必须在你身边做一些事情,如果你在这些尝试邮政编码之间卡住了一些地方,然后我们可以帮助你,祝你的尝试好运
-
你能举几行CSV文件的例子并解释一下信息在哪里吗?
标签: objective-c xcode