使用目标C从CSV文件创建阵列 [英] Creating an Array From a CSV File Using Objective C

查看:81
本文介绍了使用目标C从CSV文件创建阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码的新手,如果这是一个简单的问题,请原谅.

I'm new to coding so please excuse me if this seems like a simple question.

我正在尝试在地图上绘制坐标.

I'm trying to plot coordinates on a map.

我想读取CSV文件并将信息传递到两个单独的数组.

I want to read a CSV file and pass the information to two separate arrays.

第一个数组将是NSArray * towerInfo(包含纬度,经度和塔标题)

The first array will be NSArray *towerInfo (containing latitude, longitude and tower title)

第二个NSArray *区域(包含塔标题和区域),其计数索引与第一个数组相同.

the second, NSArray *region (containing tower title and region) with the same count index as the first array.

本质上,我认为我需要;

Essentially, I believe I need to;

1)将文件读取为字符串.....

1) read the file to a string.....

2)将字符串分成一个临时数组,每个/n/r分开……

2) divide the string into a temporary array separating at every /n/r......

3)循环遍历temp数组,并在每次将此信息附加到两个主存储阵列之前创建一个塔和区域对象.

3) loop through the temp array and create a tower and region object each time before appending this information to the two main storage arrays.

这是正确的过程吗,如果可以,那么任何人都可以发布一些示例代码,因为我真的在努力做到这一点.

Is this the right process and if so is there anyone out there who can post some sample code as I'm really struggling to get this right.

谢谢大家.

克里斯.

我已对此进行了编辑,以显示我的代码示例.我遇到的问题是我收到

I have edited this to show an example of my code. I am having the problem that I'm receiving warnings saying

1)'dataStr'的本地声明隐藏了实例变量. 2)'array'的本地声明隐藏了实例变量.

1) "the local declaration of 'dataStr' hides instance variable. 2) "the local declaration of 'array' hides instance variable.

我想我理解这些是什么意思,但我不知道该如何解决.该程序可以编译并运行,但是日志告诉我数组为空".

I think I understand what these mean but I don't know how to get around it. The program compiles and runs but the log tells me that the "array is null."

#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

}

任何其他帮助将不胜感激.感谢到目前为止的答复.

Any additional help would be much appreciated. Thanks for the responses so far.

推荐答案

NSString* fileContents = [NSString stringWithContentsOfURL:filename ...];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (...
    NSString* row = [rows objectAtIndex:n];
    NSArray* columns = [row componentsSeparatedByString:@","];
...

您可能想抛出一些"stringTrimmingCharactersInSet"调用以修剪空白.

You'll probably want to throw in a few "stringTrimmingCharactersInSet" calls to trim whitespace.

这篇关于使用目标C从CSV文件创建阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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