objective-c – 使用Objective C从CSV文件创建数组
发布时间:2020-12-15 01:48:52 所属栏目:百科 来源:网络整理
导读:我是编码的新手所以请原谅我,如果这看起来像一个简单的问题. 我正在尝试在地图上绘制坐标. 我想读取CSV文件并将信息传递给两个单独的数组. 第一个数组将是NSArray * towerInfo(包含纬度,经度和塔标题) 第二个,NSArray *区域(包含塔标题和区域)与第一个数组具
我是编码的新手所以请原谅我,如果这看起来像一个简单的问题.
我正在尝试在地图上绘制坐标. 我想读取CSV文件并将信息传递给两个单独的数组. 第一个数组将是NSArray * towerInfo(包含纬度,经度和塔标题) 第二个,NSArray *区域(包含塔标题和区域)与第一个数组具有相同的计数索引. 基本上,我相信我需要; 1)将文件读取为字符串….. 2)将字符串分成每个/ n / r分隔的临时数组…… 3)循环访问临时数组并在每次将此信息附加到两个主存储阵列之前创建塔和区域对象. 这是正确的过程,如果有的话,那里有人可以发布一些示例代码,因为我真的很难做到这一点. 感谢所有提前. 克里斯. 我编辑了这个以显示我的代码示例.我遇到的问题是我收到警告说 1)“’dataStr’的本地声明隐藏了实例变量. 我想我明白这些意思,但我不知道如何绕过它.程序编译并运行但日志告诉我“数组为空”. #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 } 任何额外的帮助将不胜感激.感谢到目前为止的回复. 解决方法NSString* fileContents = [NSString stringWithContentsOfURL:filename ...]; NSArray* rows = [fileContents componentsSeparatedByString:@"n"]; for (... NSString* row = [rows objectAtIndex:n]; NSArray* columns = [row componentsSeparatedByString:@","]; ... 您可能希望抛出一些“stringTrimmingCharactersInSet”调用来修剪空格. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |